/*
* Copyright (C) 2001, 2002 Robert MacGrogan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* $Archive: SourceJammer$
* $FileName: UploaderProxy.java$
* $FileID: 4202$
*
* Last change:
* $AuthorName: Rob MacGrogan$
* $Date: 4/23/03 5:04 PM$
* $Comment: Replaced GPL header with LGPL header.$
*/
package org.sourcejammer.client;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;
import org.sourcejammer.util.FileUtil;
import org.sourcejammer.util.SJFileDataSource;
import org.sourcejammer.util.SendBytesDataSource;
import org.sourcejammer.util.SourceJammerConnectionException;
import org.sourcejammer.util.SendBytes;
import org.apache.soap.transport.*;
import org.apache.soap.transport.http.*;
import org.sourcejammer.client.gui.conf.UserPrefs;
/**
* Title: $FileName: UploaderProxy.java$
* @version $VerNum: 4$
* @author $AuthorName: Rob MacGrogan$<br><br>
*
* $Description: $<br>
* $KeyWordsOff: $<br>
*/
public class UploaderProxy {
private static SOAPMappingRegistry mapping = new SOAPMappingRegistry();
private static byte[] EMPTY_BYTES = {0};
static {
BeanSerializer ser = new BeanSerializer();
MimePartSerializer mimeSer = new MimePartSerializer();
mapping.mapTypes(Constants.NS_URI_SOAP_ENC, new QName("urn:sj-uploader", "sendbytes"), SendBytes.class, ser, ser);
mapping.mapTypes(Constants.NS_URI_SOAP_ENC, new QName("urn:sj-uploader", "datahandler"), DataHandler.class, mimeSer, mimeSer);
}
private static final String TARGET_URI = "urn:Uploader";
private static final class Methods {
public static final String GET_FILE_UPLOAD_ID = "getFileUploadID";
public static final String UPLOAD_BYTES = "uploadBytes";
public static final String UPLOAD_FILE = "uploadFile";
}
private Call call = null;
private URL url = null;
public UploaderProxy() {
initCall();
}
public UploaderProxy(String url) throws MalformedURLException {
this.url = new URL(url);
initCall();
}
public UploaderProxy(URL url) {
this.url = url;
}
private void initCall() {
call = new Call();
call.setSOAPMappingRegistry(mapping);
call.setTargetObjectURI(TARGET_URI);
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
SOAPHTTPConnection con = new SOAPHTTPConnection();
con.setMaintainSession(true);
UserPrefs prefs = UserPrefs.getInstance();
boolean proxyEnabled = prefs.getBoolean(UserPrefs.PROXY_ENABLED, false);
if (proxyEnabled) {
String host = prefs.getString(UserPrefs.PROXY_HOST, "x");
int port = Integer.parseInt(prefs.getString(UserPrefs.PROXY_PORT, "8080"));
con.setProxyHost(host);
con.setProxyPort(port);
if (prefs.getBoolean(UserPrefs.PROXY_USE_PASSWORD, false)){
String user = prefs.getString(UserPrefs.PROXY_USER, "");
con.setProxyUserName(user);
con.setProxyPassword(SOAPPortal.getProxyPassword());
}
}
call.setSOAPTransport(con);
}
public void setURL(String url) throws MalformedURLException {
this.url = new URL(url);
}
public void setURL(URL url) {
this.url = url;
}
public URL getURL() {
return this.url;
}
public long getFileUploadID() throws SourceJammerConnectionException {
call.setMethodName(Methods.GET_FILE_UPLOAD_ID);
Vector params = new Vector();
call.setParams(params);
Response response = null;
try {
response = call.invoke(url, "");
}
catch (SOAPException ex) {
throw new SourceJammerConnectionException("Problem with SOAP communication.", ex);
}
long uploadID = -1;
if (!response.generatedFault()) {
Parameter ret = response.getReturnValue();
Long lngID = (Long) ret.getValue();
uploadID = lngID.longValue();
}
else {
Fault fault = response.getFault();
StringBuffer strException = new StringBuffer();
strException
.append("A fault was generated:\r\n")
.append(" Fault Code = ")
.append(fault.getFaultCode())
.append("\r\n")
.append(" Fault String = ")
.append(fault.getFaultString());
throw new SourceJammerConnectionException(strException.toString());
}
return uploadID;
}
public long uploadFile(File fl) throws SourceJammerConnectionException{
long uploadID = -1;
SendBytes sender = new SendBytes();
SJFileDataSource dataSource = new SJFileDataSource(fl);
sender.setData(new DataHandler(dataSource));
sender.setEof(true);
call.setMethodName(Methods.UPLOAD_FILE);
Vector params = new Vector();
params.addElement(new Parameter("by", SendBytes.class, sender, null));
call.setParams(params);
Response response = null;
try {
response = call.invoke(url, "");
}
catch (SOAPException ex) {
throw new SourceJammerConnectionException("Problem with SOAP communication.", ex);
}
if (!response.generatedFault()) {
Parameter ret = response.getReturnValue();
Long lngID = (Long) ret.getValue();
uploadID = lngID.longValue();
}
else {
Fault fault = response.getFault();
StringBuffer strException = new StringBuffer();
strException
.append("A fault was generated:\r\n")
.append(" Fault Code = ")
.append(fault.getFaultCode())
.append("\r\n")
.append(" Fault String = ")
.append(fault.getFaultString());
throw new SourceJammerConnectionException(strException.toString());
}
return uploadID;
}
/**
* Streams bytes into the FileUploader.
*
* @param by -- a byte array being streamed in. This is a chunk of a file.
*/
public void uploadBytes(byte[] by) throws SourceJammerConnectionException {
uploadBytes(by, false);
}
/**
* Streams bytes into the FileUploader.
*
* @param by -- a byte array being streamed in. This is a chunk of a file.
* @param eol -- if true, tells FileUploader that this is the last chunk
* of bytes in the file. FileUploader closes the output stream.
*/
public void uploadBytes(byte[] by, boolean eof) throws SourceJammerConnectionException {
SendBytes bySend = new SendBytes();
SendBytesDataSource source = new SendBytesDataSource();
// try{
// FileUtil.sendBytesToSendBytesDataSource(source, by);
// }
// catch (IOException ex){
// throw new SourceJammerConnectionException(ex.getMessage(), ex);
// }
source.setBytes(by);
bySend.setData(new DataHandler(source));
bySend.setEof(eof);
call.setMethodName(Methods.UPLOAD_BYTES);
Vector params = new Vector();
params.addElement(new Parameter("by", SendBytes.class, bySend, null));
call.setParams(params);
Response response = null;
try {
response = call.invoke(url, "");
}
catch (SOAPException ex) {
throw new SourceJammerConnectionException("Problem with SOAP communication.", ex);
}
//Kill data source. This stupid line is required because each subsequent call
//to call.setParams with a DataSource actually just appends a new attachment
//to the call. Previous attachments are not removed. This next line
//seems to at least set previous attachment to a 1 byte length attachment,
//which is stupid, but at least does not cause Out of Memory.
source.setBytes(EMPTY_BYTES);
if (!response.generatedFault()) {
//nothing to do.
}
else {
Fault fault = response.getFault();
StringBuffer strException = new StringBuffer();
strException
.append("A fault was generated:\r\n")
.append(" Fault Code = ")
.append(fault.getFaultCode())
.append("\r\n")
.append(" Fault String = ")
.append(fault.getFaultString());
throw new SourceJammerConnectionException(strException.toString());
}
}
}
|