Finally moved MIME stuff out of helma.objectmodel.Node

This commit is contained in:
hns 2001-07-31 21:59:51 +00:00
parent bb90831541
commit 08867be3ea
2 changed files with 125 additions and 0 deletions

View file

@ -0,0 +1,80 @@
// MimePart.java
// Copyright (c) Hannes Wallnöfer 2001
package helma.util;
import java.io.*;
/**
* This represents a MIME part of a HTTP file upload
*/
public class MimePart implements Serializable {
public final String name;
public int contentLength;
public String contentType;
private byte[] content;
public MimePart (String name, byte[] content, String contentType) {
this.name = name;
this.content = content == null ? new byte[0] : content;
this.contentType = contentType;
contentLength = content == null ? 0 : content.length;
}
public byte[] getContent () {
return content;
}
public String getText () {
if (contentType == null || contentType.startsWith ("text/")) {
// todo: check for encoding
return new String (content);
} else {
return null;
}
}
public String writeToFile (String dir) {
return writeToFile (dir, null);
}
public String writeToFile (String dir, String fname) {
try {
File base = new File (dir);
// make directories if they don't exist
if (!base.exists ())
base.mkdirs ();
String filename = name;
if (fname != null) {
if (fname.indexOf (".") < 0) {
// check if we can use extension from name
int ndot = name == null ? -1 : name.lastIndexOf (".");
if (ndot > -1)
filename = fname + name.substring (ndot);
else
filename = fname;
} else {
filename = fname;
}
}
File file = new File (base, filename);
FileOutputStream fout = new FileOutputStream (file);
fout.write (getContent ());
fout.close ();
return filename;
} catch (Exception x) {
return null;
}
}
public String getName () {
return name;
}
}

View file

@ -0,0 +1,45 @@
// MimePartDataSource.java
// Copyright (c) Hannes Wallnöfer 1999-2000
package helma.util;
import javax.activation.*;
import java.io.*;
/**
* Makes MimeParts usable as Datasources in the Java Activation Framework (JAF)
*/
public class MimePartDataSource implements DataSource {
private MimePart part;
private String name;
public MimePartDataSource (MimePart part) {
this.part = part;
this.name = part.getName ();
}
public MimePartDataSource (MimePart part, String name) {
this.part = part;
this.name = name;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(part.getContent ());
}
public OutputStream getOutputStream () throws IOException {
throw new IOException ("Can't write to MimePart object.");
}
public String getContentType() {
return part.contentType;
}
public String getName () {
return name;
}
}