July 7, 2009
ICE, The Fascinator Desktop, eResearch, java
1 Comment
Time to blog my multipart file upload version 2. In this version I am no longer using the MultiPartFormOutputStream.java (see my previous blog: http://lindaocta.com/?p=185)
In this version, I use java dependencies:
Transformer_two.java code:
[html]
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.io.IOUtils;
class transformer_two{
public static void main(String [] args) {
String convertUrl = "http://ice-service.usq.edu.au/api/convert/";
if(args.length == 1) {
String fileName = args[0];
System.out.println(fileName);
File f = new File(fileName);
if(f.exists()) {
String extResult = getExtension(f);
if (!extResult.startsWith("Error")) {
convertUrl = convertUrl + extResult;
getRendition(f, convertUrl);
}
} else {
System.out.println("Error: file doesn't exist!");
}
} else {
System.out.println("Usage: java transformer fileName");
}
}
public static void getRendition (File sourceFile, String convertUrl) {
try{
HttpClient client = new HttpClient();
PostMethod filePost = new PostMethod(convertUrl);
Part[] parts = {
new StringPart("zip", "1"),
new StringPart("toc", "1"),
new StringPart("pdfLink", "1"),
new StringPart("pathext", ""),
new StringPart("template", getTemplate()),
new FilePart("file", sourceFile)
};
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
client.executeMethod(filePost);
//Get Response
InputStream is = filePost.getResponseBodyAsStream();
String[] filePart = sourceFile.getName().split("\\.");
System.out.println(sourceFile.getName());
String outputFilename = "/tmp/"+ filePart[0] + ".zip";
FileOutputStream fos = new FileOutputStream(outputFilename);
IOUtils.copy(is, fos);
is.close();
fos.close();
System.out.println("Rendition zip file is located at: " + outputFilename);
} catch (Exception e) {
System.out.println("An error occurred: " + e.getClass());
e.printStackTrace();
}
}
private static String getExtension(File fileObject) {
/** getExtension method
* @param File fileObject
* @return String
*/
String[] parts = fileObject.getName().split("\\.");
String ext = "";
if (parts.length == 2) {
ext = parts[1];
return ext;
}
return "Error: Unable to detect file extension properly!";
}
private static String getTemplate (){
String template = "<html>" +
"<head>" +
"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>" +
"<title>Default Template</title>" +
"<style type='text/css'>" +
".rendition-links { text-align: right; }" +
".body table td { vertical-align: top; }" +
"</style>" +
"<style class='sub style-css' type='text/css'></style>" +
"</head>" +
"<body>" +
"<div class='rendition-links'>" +
"<span class='ins source-link'></span>" +
"<span class='ins slide-link'></span>" +
"<span class='ins pdf-rendition-link'></span>" +
"</div>" +
"<h1 class='ins title'></h1>" +
"<div class='ins page-toc'></div>" +
"<div class='ins body'></div>" +
"</body>" +
"</html>";
return template;
}
}
To run the new transformer_two.java:
- download the code from: transformer_two.java and save as transformer_two.java
- create “lib” folder in the same level
- Put all the jar file from the downloaded dependencies above in “lib” folder
- Compile transformer_two.java:
[html]
javac -d . -classpath ./lib/commons-httpclient-3.1.jar:./lib/commons-io-1.4.jar transformer_two.java
- To run transformer_two:
[html]
java -classpath .:./lib/commons-httpclient-3.1.jar:./lib/commons-io-1.4.jar:./lib/commons-logging-1.1.1.jar:./lib/commons-codec-1.3.jar transformer_two ABSOLUTE_PATH_TO_YOUR_DOCUMENT
July 2, 2009
ICE, The Fascinator Desktop, eResearch, java
1 Comment
Uploading Multipart file through java is not that easy. I need to write this code for the Transformer (I don’t have a chance to write the wiki yet
), the bridge between The Fascinator desktop and Ice-Service.
Since I am trying my best to remember java syntax again, I am confuse which library to use. In this blog, I will put up the first version of multipart upload code, so for those who would like to use Ice-Service to get the rendered version of your documents, you can use this code independently 
From this website http://forums.sun.com/thread.jspa?threadID=451245&forumID=31, I found a class that I can use directly, you can download the code from MultiPartFormOutputStream.java
And below are my transformer.java code:
[html]
import java.io.*;
import java.net.*;
class transformer{
public static void main(String [] args) {
String convertUrl = "http://ice-service.usq.edu.au/api/convert/";
if(args.length == 1) {
String fileName = args[0];
System.out.println(fileName);
File f = new File(fileName);
if(f.exists()) {
getRendition(convertUrl, f);
} else {
System.out.println("Error: file doesn't exist!");
}
} else {
System.out.println("Usage: java transformer fileName");
}
}
public static void getRendition (String convertUrl, String fileName) {
getRendition(convertUrl, new File(fileName));
}
public static void getRendition (String convertUrl, File sourceFile) {
String[] parts = sourceFile.getName().split("\.");
String ext = "";
if(parts.length == 2) { // check we have exactly two parts
ext = parts[1];
} else {
System.out.println("Error: Unable to detect file extension properly!");
return;
}
System.out.println("convertUrl: " + convertUrl);
convertUrl = convertUrl + ext;
System.out.println("fileName: " + sourceFile.getAbsolutePath());
try{
//Create connection
URL url = new URL(convertUrl);
//create a boundary string
String boundary = MultiPartFormOutputStream.createBoundary();
URLConnection urlConn = MultiPartFormOutputStream.createConnection(url);
urlConn.setRequestProperty("Accept", "*/*");
urlConn.setRequestProperty("Content-Type",
MultiPartFormOutputStream.getContentType(boundary));
//set some other request headers...
urlConn.setRequestProperty("Connection", "Keep-Alive");
urlConn.setRequestProperty("Cache-Control", "no-cache");
//no need to connect cuz getOutputStream() does it
MultiPartFormOutputStream out =
new MultiPartFormOutputStream(urlConn.getOutputStream(), boundary);
//Setting up all the options
out.writeField("zip", "1");
out.writeField("toc", "1");
out.writeField("pdfLink", "1");
out.writeField("pathext", "");
out.writeField("template", getTemplate());
out.writeFile("file", "application/zip", sourceFile);
out.close();
//Get Response
InputStream is = urlConn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
//zip the file and store in the /tmp directory
String outputFilename = "/tmp/rendition.zip";
FileOutputStream fos = new FileOutputStream(outputFilename);
Writer fileout = new OutputStreamWriter(fos);
int ch;
while ((ch = rd.read()) > -1) {
fileout.write((char)ch);
}
fileout.close();
} catch (Exception e) {
System.out.println("An error occurred: " + e.getClass());
//e.printStackTrace();
}
}
private static String getTemplate (){
String template = "<html>" +
"<head>" +
"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>" +
"<title>Default Template</title>" +
"<style type='text/css'>" +
".rendition-links { text-align: right; }" +
".body table td { vertical-align: top; }" +
"</style>" +
"<style class='sub style-css' type='text/css'></style>" +
"</head>" +
"<body>" +
"<div class='rendition-links'>" +
"<span class='ins source-link'></span>" +
"<span class='ins slide-link'></span>" +
"<span class='ins pdf-rendition-link'></span>" +
"</div>" +
"<h1 class='ins title'></h1>" +
"<div class='ins page-toc'></div>" +
"<div class='ins body'></div>" +
"</body>" +
"</html>";
return template;
}
}
To use the code, simply put download both transformer.java and MultiPartFormOutputStream.java in the same directory and compile transformer.java:
[html]
lindaoctalina$ javac transformer.java
lindaoctalina$ java transformer ABSOLUTE_PATH_TO_YOUR_DOCUMENT
Your file will be saved to /tmp directory called rendition.zip
In my next few blogs, I will put up my enhanced version of this transformer.java. I am not using the MultiPartFormOutputStream.java anymore. I will also blog the transformer in php version (since I have the code somewhere when I did Open Journal System project)