001 /**
002 *
003 */
004 package de.jw.cloud42.webservice;
005
006 import javax.activation.DataHandler;
007 import javax.xml.namespace.QName;
008
009 import org.apache.axiom.attachments.ByteArrayDataSource;
010 import org.apache.axiom.attachments.ConfigurableDataHandler;
011 import org.apache.axiom.om.OMAbstractFactory;
012 import org.apache.axiom.om.OMElement;
013 import org.apache.axiom.om.OMFactory;
014 import org.apache.axiom.om.OMNamespace;
015 import org.apache.axiom.om.OMText;
016 import org.apache.axis2.context.MessageContext;
017 import org.apache.axis2.context.OperationContext;
018 import org.apache.axis2.wsdl.WSDLConstants;
019
020 import de.jw.cloud42.core.domain.RemoteResult;
021 import de.jw.cloud42.core.remoting.RemoteControl;
022
023 /**
024 * Service class for Cloud42 Web service file transfer functions.
025 *
026 * @author fbitzer
027 *
028 */
029 public class Cloud42FileService {
030
031 /**
032 * Upload a file to an AMI instance.
033 * @param dnsName Hostname or IP address of remote host.
034 * @param rsaKey RSA private key to use for authentification.
035 * @param targetDir Target folder on remote machine.
036 * @param targetFilename Target filename on remote machine.
037 * @param fileData The uploaded file.
038 * @return {@link RemoteResult} with empty output if execution was successful or an
039 * exception message if not.
040 */
041 public RemoteResult uploadFile(String dnsName, String rsaKey, String targetDir, String targetFilename, byte[] fileData){
042
043 RemoteControl c = new RemoteControl();
044
045 return c.uploadFile(dnsName, rsaKey, targetDir, targetFilename, fileData);
046
047 }
048
049 /**
050 * Upload a file from an URL to an AMI instance.
051 * Can be used to transfer files from S3 buckets to an instance.
052 * Uses wget for file transfer.
053 *
054 * @param dnsName Hostname or IP address of remote host/the running instance.
055 * @param rsaKey RSA private key to use for authentification.
056 * @param targetDir Target folder on remote machine.
057 * @param targetFilename Target filename on remote machine.
058 * @param url URL for download.
059 * @return {@link RemoteResult} encapsulating the output of the wget command.
060 */
061 public RemoteResult uploadFileFromURL(String dnsName, String rsaKey, String targetDir, String targetFilename, String url){
062
063 RemoteControl c = new RemoteControl();
064
065 return c.uploadFileFromURL(dnsName, rsaKey, targetDir, targetFilename, url);
066
067 }
068
069 // /**
070 // * Download a file from a remote host.
071 // *
072 // * @param element SOAP message containing the following tags (in the namespace http://webservice.cloud42.jw.de):
073 // * <ul>
074 // * <li>dnsName: hostname or IP address of remote host.</li>
075 // * <li>rsaKey: RSA key to use.</li>
076 // * <li>file: absolute path of file to retreive.</li>
077 // * </ul>
078 // * @return MTOM-optimized SOAP message containing the requestet file.
079 // */
080 // public OMElement downloadFile(OMElement element){
081 //
082 //
083 // //parse incoming OMElement
084 // QName qn = new QName("http://webservice.cloud42.jw.de","rsaKey");
085 // String key = element.getFirstChildWithName(qn).getText();
086 //
087 // qn = new QName("http://webservice.cloud42.jw.de","dnsName");
088 // String host = element.getFirstChildWithName(qn).getText();
089 //
090 // qn = new QName("http://webservice.cloud42.jw.de","file");
091 // String file = element.getFirstChildWithName(qn).getText();
092 //
093 //
094 //
095 // //invoke business logic to retreive file.
096 // RemoteControl c = new RemoteControl();
097 //
098 // byte[] data = c.downloadFile(host, key, file);
099 //
100 //
101 // try {
102 // //create a SOAP message for the response
103 // OMFactory factory = element.getOMFactory();
104 // OMNamespace ns = factory.createOMNamespace("http://webservice.cloud42.jw.de", "web");
105 // OMElement payload = factory.createOMElement("uploadFileResponse", ns);
106 // OMElement response = factory.createOMElement("return", ns);
107 // OMElement fileElement = factory.createOMElement("file", ns);
108 //
109 //
110 // DataHandler dataHandler = new DataHandler(new ByteArrayDataSource(data));
111 // OMText textData = factory.createOMText(dataHandler, true);
112 // fileElement.addChild(textData);
113 // response.addChild(fileElement);
114 // payload.addChild(response);
115 //
116 //
117 // //enable MTOM -> Axis will automatically optimize the message
118 // MessageContext outMsgCtx = MessageContext.getCurrentMessageContext()
119 // .getOperationContext().getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
120 //
121 // outMsgCtx.setProperty(
122 // org.apache.axis2.Constants.Configuration.ENABLE_MTOM,
123 // org.apache.axis2.Constants.VALUE_TRUE);
124 //
125 // return payload;
126 //
127 //
128 // } catch (Exception ex){
129 //
130 // ex.printStackTrace();
131 //
132 // return null;
133 // }
134 //
135 // }
136
137
138 /**
139 * Download a file from a remote host.
140 *
141 * @param dnsName Hostname or IP address of remote host.
142 * @param rsaKey RSA private key to use for authentification.
143 * @param fileName full path and name of file to download.
144 *
145 * @return MTOM message with requested file as attachment.
146 */
147 public DataHandler downloadFile(String dnsName, String rsaKey, String fileName) {
148
149 RemoteControl c = new RemoteControl();
150
151 byte[] data = c.downloadFile(dnsName, rsaKey, fileName);
152
153 ByteArrayDataSource dataSource = new ByteArrayDataSource(data);
154 DataHandler dataHandler = new DataHandler(dataSource);
155
156 return dataHandler;
157
158 }
159
160 }