Tutorial 17: Writing an application that uploads a file
In this tutorial, you write an application that uploads a file to an iHub volume. When you complete this tutorial, the file appears in the reports folder, as shown in Figure 21‑17.
Figure 21‑17 The reports folder showing the finished file from this tutorial
To complete this tutorial, you need access to the following application classes and related files from Tutorial 15:
*Arguments.java
*AcController.java
*ActuateAPIEx.java
*ActuateAPILocatorEx.java
Task 1: How to upload a file
In this task, you create a Java class, AcUploadFile, that uploads a file to an iHub volume, receives a response, and prints the name of the uploaded file.If you did not create Arguments.java, ActuateAPIEx.java, and ActuateAPILocatorEx.java in Tutorial 15: Catching a SOAP message with Axis TCPMonitor, you must also create these classes as well.
1 Navigate to or create the \ActuateTraining3\IDAPI\source directory. Create a new file called\ActuateTraining11\IDAPI\source\AcUploadFile.java and open the file for editing. Add import statements for javax.xml.rpc.ServiceException, java.io.File, and java.rmi.RemoteException, add an AcUploadFile class definition, and create the uploadClass and main methods, as shown in Listing 21‑6.
Listing 21‑6 The AcUploadFile class
import javax.xml.rpc.ServiceException;
import java.io.File;
import java.rmi.RemoteException;
 
public class AcUploadFile {
public static String usage =
"java AcUploadFile [options] localFileName [encyclopediaFileName]\n"
+ "\n"
+ " Upload a file to the server\n";
public static ActuateControl actuateControl;
 
/**
* This example uploads a file to Actuate iHub volume
* @throws RemoteException
*/
public static void uploadFile (String locFileName, String encycFileName) throws RemoteException, ServiceException {
System.out.println("Uploading file: " + locFileName);
System.out.println("As file : " + encycFileName);
// set new file information
com.actuate.schemas.NewFile newFile = new com.actuate.schemas.NewFile();
newFile.setName(encycFileName);
// set MIME content id (must be same as AttachmentPart)
com.actuate.schemas.Attachment content = new com.actuate.schemas.Attachment();
content.setContentId(locFileName);
// set upload message
com.actuate.schemas.UploadFile request = new com.actuate.schemas.UploadFile();
request.setNewFile(newFile);
request.setContent(content);
// use input file as data source
javax.activation.DataHandler dhSource = new javax.activation.DataHandler(new javax.activation.FileDataSource(locFileName));
// set attachment in call
org.apache.axis.attachments.AttachmentPart attachmentPart = new org.apache.axis.attachments.AttachmentPart();
attachmentPart.setDataHandler(dhSource);
attachmentPart.setContentId(locFileName);
// call upload file
com.actuate.schemas.UploadFileResponse response = actuateControl.uploadFile(request, attachmentPart);
// print out the file id of the encyclopedia file
System.out.println("Uploaded " + locFileName + " with fileid: " + response.getFileId());
}
 
public static void main(String[] args) {
// upload settings
String localFileName;
String encyclopediaFileName;
Arguments.usage = usage;
// get command line arguments
Arguments arguments = new Arguments(args);
localFileName = arguments.getArgument();
if ( !new File(localFileName).exists() ) {
System.out.println("File not found: " + localFileName );
return;
}
// get optional parameter
encyclopediaFileName = arguments.getOptionalArgument(new
File(localFileName).getName());
try {
actuateControl = new ActuateControl(arguments.getURL());
// login to actuate server
actuateControl.setUsername(arguments.getUsername());
actuateControl.setPassword(arguments.getPassword());
actuateControl.setVolume(arguments.getVolume());
actuateControl.login();
uploadFile(localFileName,encyclopediaFileName);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
1 To instantiate the NewFile object, after the following line:
// set new file information
type:
com.actuate.schemas.NewFile newFile =
new com.actuate.schemas.NewFile( );
newFile.setName(encycFileName);
2 To instantiate the Attachment object and specify the file name, after the following line:
// set MIME content id (must be same as AttachmentPart)
type:
com.actuate.schemas.Attachment content =
new com.actuate.schemas.Attachment( );
content.setContentId(locFileName);
3 To instantiate the UploadFile object and specify the name and location of the file, after the following line:
// set upload message
type:
com.actuate.schemas.UploadFile request =
new com.actuate.schemas.UploadFile( );
request.setNewFile(newFile);
request.setContent(content);
4 To set up the data handler using the input file as the data source, after the following line:
// use input file as data source
type:
javax.activation.DataHandler dhSource =
new javax.activation.DataHandler(new
javax.activation.FileDataSource(locFileName));
5 To embed the attachment in the response, after the following line:
// set attachment in call
type:
org.apache.axis.attachments.AttachmentPart attachmentPart =
new org.apache.axis.attachments.AttachmentPart( );
attachmentPart.setDataHandler(dhSource);
attachmentPart.setContentId(locFileName);
6 To upload the file you receive in an Encyclopedia volume response, after the following line:
// call upload file
type:
com.actuate.schemas.UploadFileResponse response =
actuateControl.uploadFile(request, attachmentPart);
7 To print the name of the uploaded file, after the following line:
// print out the file id of the encyclopedia file
type:
System.out.println("Uploaded " + locFileName + " with fileid: " + response.getFileId());
2 In the \ActuateTraining3\IDAPI\source directory, open the file called AcController.java, created in Tutorial 15: Catching a SOAP message with Axis TCPMonitor, for editing. If AcController.java does not exist, create a new file called AcController.java as instructed in Tutorial 15. Add a method to upload a file as shown in Listing 21‑7.
Listing 21‑7 AcController with a uploadFile method
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Calendar;
import javax.xml.rpc.ServiceException;
import com.actuate.schemas.*;
 
public class AcController implements java.io.Serializable {
// control variables
public String authenticationId = null;
public String username = "Administrator";
private String password = "";
 
// server settings
public String actuateServerURL = "http://localhost:8000/";
 
// proxy operation
public ActuateSoapBindingStub proxy;
public ActuateAPIEx actuateAPI;
 
/**
* No-argument Constructor
*/
public AcController() throws MalformedURLException, ServiceException {
actuateAPI = new ActuateAPILocatorEx();
setActuateServerURL(actuateServerURL);
}
 
/**
* Constructor accepting serverURL as a parameter
*/
public AcController(String serverURL)
throws MalformedURLException, ServiceException {
actuateAPI = new ActuateAPILocatorEx();
setActuateServerURL(serverURL);
}
 
/**
* Create a new call object that can be used to send SOAP
* message to actuate server
* @return Call
* @throws ServiceException
*/
public org.apache.axis.client.Call createCall() throws ServiceException {
org.apache.axis.client.Call call =
(org.apache.axis.client.Call) actuateAPI.createCall();
call.setTargetEndpointAddress(this.actuateServerURL);
return call;
}
 
/**
* Login to actuate server with username and password
* Return true if login success
*/
public boolean login() {
boolean success = true;
com.actuate.schemas.Login request = new com.actuate.schemas.Login();
request.setPassword(password);
request.setUser(username);
try {
actuateAPI.setAuthId(null);
com.actuate.schemas.LoginResponse response = proxy.login(request);
authenticationId = response.getAuthId();
actuateAPI.setAuthId(authenticationId);
} catch (java.rmi.RemoteException e) {
// login failed
success = false;
}
return success;
}
 
/**
* Sets the ActuateServerURL
* @param serverURL The actuate server url to set
*/
public void setActuateServerURL(String serverURL)
throws MalformedURLException, ServiceException {
if ((proxy == null) || !serverURL.equals(actuateServerURL)) {
if (serverURL != null)
actuateServerURL = serverURL;
System.out.println("Setting server to " + actuateServerURL);
proxy = (ActuateSoapBindingStub)
actuateAPI.getActuateSoapPort(
new java.net.URL(actuateServerURL));
}
}
 
/**
* Sets the password.
* @param password The password to set
*/
public void setPassword(String password) {
if (password == null)
password = "";
this.password = password;
}
 
/**
* Sets the username.
* @param username The username to set
*/
public void setUsername(String username) {
if (username == null)
return;
this.username = username;
}
 
/**
* Returns the password.
* @return String
*/
public String getPassword() {
return password;
}
 
/**
* Returns the username.
* @return String
*/
public String getUsername() {
return username;
}
 
public com.actuate.schemas.UploadFileResponse uploadFile(
com.actuate.schemas.UploadFile request,
org.apache.axis.attachments.AttachmentPart attachmentPart)
throws RemoteException, RemoteException, ServiceException {
org.apache.axis.client.Call call = createCall();
 
call.addParameter(
new javax.xml.namespace.QName(
"http://schemas.actuate.com/actuate10",
"UploadFile"),
new javax.xml.namespace.QName(
"http://schemas.actuate.com/actuate10",
"UploadFile"),
com.actuate.schemas.UploadFile.class,
javax.xml.rpc.ParameterMode.IN);
 
call.setReturnType(
new javax.xml.namespace.QName(
"http://schemas.actuate.com/actuate10",
"UploadFileResponse"));
 
call.setUseSOAPAction(true);
call.setSOAPActionURI("");
call.setEncodingStyle(null);
call.setProperty(
org.apache.axis.AxisEngine.PROP_DOMULTIREFS,
Boolean.FALSE);
call.setProperty(
org.apache.axis.client.Call.SEND_TYPE_ATTR,
Boolean.FALSE);
call.setOperationStyle("document");
call.setOperationName(
new javax.xml.namespace.QName(
"http://schemas.actuate.com/actuate10",
"UploadFile"));
 
// set the actual MIME attachment
call.addAttachmentPart(attachmentPart);
 
Object resp = call.invoke(new Object[] { request });
 
if (resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException) resp;
} else {
try {
return (com.actuate.schemas.UploadFileResponse) resp;
} catch (java.lang.Exception e) {
return (com.actuate.schemas.UploadFileResponse) org.apache.axis.utils.JavaUtils.convert(resp, com.actuate.schemas.UploadFileResponse.class);
}
}
}
}
3 Save and close all files.
Task 2: How to compile and run the application to upload a file
In this task, you compile and run the upload file application. The run command uses the following syntax:
java AcUploadFile -h http://localhost:8000 <localFileName> [<encyclopediaFileName>]
1 Open a command prompt window.
2 At the command prompt, type:
cd C:\ActuateTraining11\IDAPI
3 Add the axis client libraries, build path, and source path to the CLASSPATH environment variable.At the command prompt, type:
set CLASSPATH=C:\Actuate3\ServerIntTech3\Web Services\Examples\Axis Client\lib\*;C:\Program Files\Actuate11\ServerIntTech\Web Services\Examples\Axis Client\build;C:\Program Files\Actuate11\ServerIntTech\Web Services\Examples\Axis Client\source;
Press Enter.
4 To compile the Java application, type:
javac -d build source/AcUploadFile.javasource/AcController.java
If you did not compile Arguments.java, ActuateAPIEx.java, and ActuateAPILocatorEx.java in Tutorial 15: Catching a SOAP message with Axis TCPMonitor, you must also compile these classes.
5 To run the Java application, type:
java AcUploadFile -h http://localhost:8000 reports/Employee_Directory.rptdesign /Home/administrator/report/Employee_Directory.rptdesign
The Java application writes the following messages, shown in Figure 21‑18, to the command prompt window when an Encyclopedia volume login succeeds and the file upload occurs.
Figure 21‑18 Message indicating that login succeeds and a file is uploaded
How to verify the file upload
In this task, you use the Visualization Platform to view the results of the upload file operation.
1 Open iHub Visualization Platform.
2 Log in as Administrator.
The files and folders belonging to administrator appear.
3 Choose /report.
4 Verify that /report contains the new version of Employee_Directory, as shown in Figure 21‑19.
Figure 21‑19 The contents of the report folder showing Employee_Directory