use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project convertigo by convertigo.
the class HTTPUploadStatement method handleUpload.
public void handleUpload(HttpMethod method, Context context) throws EngineException {
if (method instanceof PostMethod) {
try {
PostMethod uploadMethod = (PostMethod) method;
MultipartRequestEntity mp = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), uploadMethod.getParams());
HeaderName.ContentType.setRequestHeader(method, mp.getContentType());
uploadMethod.setRequestEntity(mp);
} catch (Exception e) {
throw new EngineException("(HTTPUploadStatement) failed to handleUpload", e);
}
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project convertigo by convertigo.
the class RemoteAdmin method deployArchive.
public void deployArchive(File archiveFile, boolean bAssembleXsl) throws RemoteAdminException {
String deployServiceURL = (bHttps ? "https" : "http") + "://" + serverBaseUrl + "/admin/services/projects.Deploy?bAssembleXsl=" + bAssembleXsl;
PostMethod deployMethod = null;
Protocol myhttps = null;
try {
if (bHttps && bTrustAllCertificates) {
ProtocolSocketFactory socketFactory = MySSLSocketFactory.getSSLSocketFactory(null, null, null, null, true);
myhttps = new Protocol("https", socketFactory, serverPort);
Protocol.registerProtocol("https", myhttps);
hostConfiguration = httpClient.getHostConfiguration();
hostConfiguration.setHost(host, serverPort, myhttps);
httpClient.setHostConfiguration(hostConfiguration);
}
deployMethod = new PostMethod(deployServiceURL);
Part[] parts = { new FilePart(archiveFile.getName(), archiveFile) };
deployMethod.setRequestEntity(new MultipartRequestEntity(parts, deployMethod.getParams()));
int returnCode = httpClient.executeMethod(deployMethod);
String httpResponse = deployMethod.getResponseBodyAsString();
if (returnCode == HttpStatus.SC_OK) {
Document domResponse;
try {
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
domResponse = parser.parse(new InputSource(new StringReader(httpResponse)));
domResponse.normalize();
NodeList nodeList = domResponse.getElementsByTagName("error");
if (nodeList.getLength() != 0) {
Element errorNode = (Element) nodeList.item(0);
Element errorMessage = (Element) errorNode.getElementsByTagName("message").item(0);
Element exceptionName = (Element) errorNode.getElementsByTagName("exception").item(0);
Element stackTrace = (Element) errorNode.getElementsByTagName("stacktrace").item(0);
if (errorMessage != null) {
throw new RemoteAdminException(errorMessage.getTextContent(), exceptionName.getTextContent(), stackTrace.getTextContent());
} else {
throw new RemoteAdminException("An unexpected error has occured during the Convertigo project deployment: \n" + "Body content: \n\n" + XMLUtils.prettyPrintDOMWithEncoding(domResponse, "UTF-8"));
}
}
} catch (ParserConfigurationException e) {
throw new RemoteAdminException("Unable to parse the Convertigo server response: \n" + e.getMessage() + ".\n" + "Received response: " + httpResponse);
} catch (IOException e) {
throw new RemoteAdminException("An unexpected error has occured during the Convertigo project deployment.\n" + "(IOException) " + e.getMessage() + "\n" + "Received response: " + httpResponse, e);
} catch (SAXException e) {
throw new RemoteAdminException("Unable to parse the Convertigo server response: " + e.getMessage() + ".\n" + "Received response: " + httpResponse);
}
} else {
decodeResponseError(httpResponse);
}
} catch (HttpException e) {
throw new RemoteAdminException("An unexpected error has occured during the Convertigo project deployment.\n" + "Cause: " + e.getMessage(), e);
} catch (IOException e) {
throw new RemoteAdminException("Unable to reach the Convertigo server: \n" + "(IOException) " + e.getMessage(), e);
} catch (Exception e) {
throw new RemoteAdminException("Unable to reach the Convertigo server: \n" + "(Exception) " + e.getMessage(), e);
} finally {
Protocol.unregisterProtocol("https");
if (deployMethod != null)
deployMethod.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project convertigo by convertigo.
the class HttpConnector method getData.
public byte[] getData(Context context, String sUrl) throws IOException, EngineException {
HttpMethod method = null;
try {
// Fire event for plugins
long t0 = System.currentTimeMillis();
Engine.theApp.pluginsManager.fireHttpConnectorGetDataStart(context);
Engine.logBeans.trace("(HttpConnector) Retrieving data as a bytes array...");
Engine.logBeans.debug("(HttpConnector) Connecting to: " + sUrl);
// Setting the referer
referer = sUrl;
Engine.logBeans.debug("(HttpConnector) Https: " + https);
URL url = new URL(sUrl);
String host = url.getHost();
int port = url.getPort();
if (sUrl.toLowerCase().startsWith("https:")) {
if (!https) {
Engine.logBeans.debug("(HttpConnector) Setting up SSL properties");
certificateManager.collectStoreInformation(context);
}
if (port == -1)
port = 443;
Engine.logBeans.debug("(HttpConnector) Host: " + host + ":" + port);
Engine.logBeans.debug("(HttpConnector) CertificateManager has changed: " + certificateManager.hasChanged);
if (certificateManager.hasChanged || (!host.equalsIgnoreCase(hostConfiguration.getHost())) || (hostConfiguration.getPort() != port)) {
Engine.logBeans.debug("(HttpConnector) Using MySSLSocketFactory for creating the SSL socket");
Protocol myhttps = new Protocol("https", MySSLSocketFactory.getSSLSocketFactory(certificateManager.keyStore, certificateManager.keyStorePassword, certificateManager.trustStore, certificateManager.trustStorePassword, this.trustAllServerCertificates), port);
hostConfiguration.setHost(host, port, myhttps);
}
sUrl = url.getFile();
Engine.logBeans.debug("(HttpConnector) Updated URL for SSL purposes: " + sUrl);
} else {
Engine.logBeans.debug("(HttpConnector) Host: " + host + ":" + port);
hostConfiguration.setHost(host, port);
}
// Retrieving httpState
getHttpState(context);
// Proxy configuration
Engine.theApp.proxyManager.setProxy(hostConfiguration, httpState, url);
AbstractHttpTransaction httpTransaction = (AbstractHttpTransaction) context.transaction;
// Retrieve HTTP method
HttpMethodType httpVerb = httpTransaction.getHttpVerb();
String sHttpVerb = httpVerb.name();
final String sCustomHttpVerb = httpTransaction.getCustomHttpVerb();
if (sCustomHttpVerb.length() > 0) {
Engine.logBeans.debug("(HttpConnector) HTTP verb: " + sHttpVerb + " overridden to '" + sCustomHttpVerb + "'");
switch(httpVerb) {
case GET:
method = new GetMethod(sUrl) {
@Override
public String getName() {
return sCustomHttpVerb;
}
};
break;
case POST:
method = new PostMethod(sUrl) {
@Override
public String getName() {
return sCustomHttpVerb;
}
};
break;
case PUT:
method = new PutMethod(sUrl) {
@Override
public String getName() {
return sCustomHttpVerb;
}
};
break;
case DELETE:
method = new DeleteMethod(sUrl) {
@Override
public String getName() {
return sCustomHttpVerb;
}
};
break;
case HEAD:
method = new HeadMethod(sUrl) {
@Override
public String getName() {
return sCustomHttpVerb;
}
};
break;
case OPTIONS:
method = new OptionsMethod(sUrl) {
@Override
public String getName() {
return sCustomHttpVerb;
}
};
break;
case TRACE:
method = new TraceMethod(sUrl) {
@Override
public String getName() {
return sCustomHttpVerb;
}
};
break;
}
} else {
Engine.logBeans.debug("(HttpConnector) HTTP verb: " + sHttpVerb);
switch(httpVerb) {
case GET:
method = new GetMethod(sUrl);
break;
case POST:
method = new PostMethod(sUrl);
break;
case PUT:
method = new PutMethod(sUrl);
break;
case DELETE:
method = new DeleteMethod(sUrl);
break;
case HEAD:
method = new HeadMethod(sUrl);
break;
case OPTIONS:
method = new OptionsMethod(sUrl);
break;
case TRACE:
method = new TraceMethod(sUrl);
break;
}
}
// Setting HTTP parameters
boolean hasUserAgent = false;
for (List<String> httpParameter : httpParameters) {
String key = httpParameter.get(0);
String value = httpParameter.get(1);
if (key.equalsIgnoreCase("host") && !value.equals(host)) {
value = host;
}
if (!key.startsWith(DYNAMIC_HEADER_PREFIX)) {
method.setRequestHeader(key, value);
}
if (HeaderName.UserAgent.is(key)) {
hasUserAgent = true;
}
}
// set user-agent header if not found
if (!hasUserAgent) {
HeaderName.UserAgent.setRequestHeader(method, getUserAgent(context));
}
// Setting POST or PUT parameters if any
Engine.logBeans.debug("(HttpConnector) Setting " + httpVerb + " data");
if (method instanceof EntityEnclosingMethod) {
EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) method;
AbstractHttpTransaction transaction = (AbstractHttpTransaction) context.requestedObject;
if (doMultipartFormData) {
RequestableHttpVariable body = (RequestableHttpVariable) httpTransaction.getVariable(Parameter.HttpBody.getName());
if (body != null && body.getDoFileUploadMode() == DoFileUploadMode.multipartFormData) {
String stringValue = httpTransaction.getParameterStringValue(Parameter.HttpBody.getName());
String filepath = Engine.theApp.filePropertyManager.getFilepathFromProperty(stringValue, getProject().getName());
File file = new File(filepath);
if (file.exists()) {
HeaderName.ContentType.setRequestHeader(method, contentType);
entityEnclosingMethod.setRequestEntity(new FileRequestEntity(file, contentType));
} else {
throw new FileNotFoundException(file.getAbsolutePath());
}
} else {
List<Part> parts = new LinkedList<Part>();
for (RequestableVariable variable : transaction.getVariablesList()) {
if (variable instanceof RequestableHttpVariable) {
RequestableHttpVariable httpVariable = (RequestableHttpVariable) variable;
if ("POST".equals(httpVariable.getHttpMethod())) {
Object httpObjectVariableValue = transaction.getVariableValue(httpVariable.getName());
if (httpVariable.isMultiValued()) {
if (httpObjectVariableValue instanceof Collection<?>) {
for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) {
addFormDataPart(parts, httpVariable, httpVariableValue);
}
}
} else {
addFormDataPart(parts, httpVariable, httpObjectVariableValue);
}
}
}
}
MultipartRequestEntity mre = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), entityEnclosingMethod.getParams());
HeaderName.ContentType.setRequestHeader(method, mre.getContentType());
entityEnclosingMethod.setRequestEntity(mre);
}
} else if (MimeType.TextXml.is(contentType)) {
final MimeMultipart[] mp = { null };
for (RequestableVariable variable : transaction.getVariablesList()) {
if (variable instanceof RequestableHttpVariable) {
RequestableHttpVariable httpVariable = (RequestableHttpVariable) variable;
if (httpVariable.getDoFileUploadMode() == DoFileUploadMode.MTOM) {
Engine.logBeans.trace("(HttpConnector) Variable " + httpVariable.getName() + " detected as MTOM");
MimeMultipart mimeMultipart = mp[0];
try {
if (mimeMultipart == null) {
Engine.logBeans.debug("(HttpConnector) Preparing the MTOM request");
mimeMultipart = new MimeMultipart("related; type=\"application/xop+xml\"");
MimeBodyPart bp = new MimeBodyPart();
bp.setText(postQuery, "UTF-8");
bp.setHeader(HeaderName.ContentType.value(), contentType);
mimeMultipart.addBodyPart(bp);
}
Object httpObjectVariableValue = transaction.getVariableValue(httpVariable.getName());
if (httpVariable.isMultiValued()) {
if (httpObjectVariableValue instanceof Collection<?>) {
for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) {
addMtomPart(mimeMultipart, httpVariable, httpVariableValue);
}
}
} else {
addMtomPart(mimeMultipart, httpVariable, httpObjectVariableValue);
}
mp[0] = mimeMultipart;
} catch (Exception e) {
Engine.logBeans.warn("(HttpConnector) Failed to add MTOM part for " + httpVariable.getName(), e);
}
}
}
}
if (mp[0] == null) {
entityEnclosingMethod.setRequestEntity(new StringRequestEntity(postQuery, MimeType.TextXml.value(), "UTF-8"));
} else {
Engine.logBeans.debug("(HttpConnector) Commit the MTOM request with the ContentType: " + mp[0].getContentType());
HeaderName.ContentType.setRequestHeader(method, mp[0].getContentType());
entityEnclosingMethod.setRequestEntity(new RequestEntity() {
@Override
public void writeRequest(OutputStream outputStream) throws IOException {
try {
mp[0].writeTo(outputStream);
} catch (MessagingException e) {
new IOException(e);
}
}
@Override
public boolean isRepeatable() {
return true;
}
@Override
public String getContentType() {
return mp[0].getContentType();
}
@Override
public long getContentLength() {
return -1;
}
});
}
} else {
String charset = httpTransaction.getComputedUrlEncodingCharset();
HeaderName.ContentType.setRequestHeader(method, contentType);
entityEnclosingMethod.setRequestEntity(new StringRequestEntity(postQuery, contentType, charset));
}
}
// Getting the result
Engine.logBeans.debug("(HttpConnector) HttpClient: getting response body");
byte[] result = executeMethod(method, context);
long length = result != null ? result.length : 0;
if (context.transaction instanceof DownloadHttpTransaction) {
try {
length = (long) context.get("__downloadedFileLength");
} catch (Exception e) {
}
}
Engine.logBeans.debug("(HttpConnector) Total read bytes: " + length);
// Fire event for plugins
long t1 = System.currentTimeMillis();
Engine.theApp.pluginsManager.fireHttpConnectorGetDataEnd(context, t0, t1);
StringBuilder sb = new StringBuilder();
sb.append("HTTP result {ContentType: " + context.contentType + ", Length: " + length + "}\n\n");
if (result != null && context.contentType != null && (context.contentType.startsWith("text/") || context.contentType.startsWith("application/xml") || context.contentType.startsWith("application/json"))) {
sb.append(new String(result, "UTF-8"));
}
fireDataChanged(new ConnectorEvent(this, sb.toString()));
return result;
} finally {
if (method != null)
method.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project alfresco-repository by Alfresco.
the class HttpClientTransmitterImpl method sendContent.
/**
*/
public void sendContent(Transfer transfer, Set<ContentData> data) throws TransferException {
if (log.isDebugEnabled()) {
log.debug("send content to transfer:" + transfer);
}
TransferTarget target = transfer.getTransferTarget();
PostMethod postContentRequest = getPostMethod();
try {
HostConfiguration hostConfig = getHostConfig(target);
HttpState httpState = getHttpState(target);
try {
postContentRequest.setPath(target.getEndpointPath() + "/post-content");
// Put the transferId on the query string
postContentRequest.setQueryString(new NameValuePair[] { new NameValuePair("transferId", transfer.getTransferId()) });
// Put the transferId on the query string
postContentRequest.setQueryString(new NameValuePair[] { new NameValuePair("transferId", transfer.getTransferId()) });
Part[] parts = new Part[data.size()];
int index = 0;
for (ContentData content : data) {
String contentUrl = content.getContentUrl();
String fileName = TransferCommons.URLToPartName(contentUrl);
log.debug("content partName: " + fileName);
parts[index++] = new ContentDataPart(getContentService(), fileName, content);
}
MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, postContentRequest.getParams());
postContentRequest.setRequestEntity(requestEntity);
int responseStatus = httpClient.executeMethod(hostConfig, postContentRequest, httpState);
checkResponseStatus("sendContent", responseStatus, postContentRequest);
if (log.isDebugEnabled()) {
log.debug("sent content");
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
String error = "Failed to execute HTTP request to target";
log.debug(error, e);
throw new TransferException(MSG_HTTP_REQUEST_FAILED, new Object[] { "sendContent", target.toString(), e.toString() }, e);
}
} finally {
postContentRequest.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project alfresco-repository by Alfresco.
the class HttpClientTransmitterImpl method sendManifest.
public void sendManifest(Transfer transfer, File manifest, OutputStream result) throws TransferException {
TransferTarget target = transfer.getTransferTarget();
PostMethod postSnapshotRequest = getPostMethod();
MultipartRequestEntity requestEntity;
if (log.isDebugEnabled()) {
log.debug("does manifest exist? " + manifest.exists());
log.debug("sendManifest file : " + manifest.getAbsoluteFile());
}
try {
HostConfiguration hostConfig = getHostConfig(target);
HttpState httpState = getHttpState(target);
try {
postSnapshotRequest.setPath(target.getEndpointPath() + "/post-snapshot");
// Put the transferId on the query string
postSnapshotRequest.setQueryString(new NameValuePair[] { new NameValuePair("transferId", transfer.getTransferId()) });
// TODO encapsulate the name of the manifest part
// And add the manifest file as a "part"
Part file = new FilePart(TransferCommons.PART_NAME_MANIFEST, manifest);
requestEntity = new MultipartRequestEntity(new Part[] { file }, postSnapshotRequest.getParams());
postSnapshotRequest.setRequestEntity(requestEntity);
int responseStatus = httpClient.executeMethod(hostConfig, postSnapshotRequest, httpState);
checkResponseStatus("sendManifest", responseStatus, postSnapshotRequest);
InputStream is = postSnapshotRequest.getResponseBodyAsStream();
final ReadableByteChannel inputChannel = Channels.newChannel(is);
final WritableByteChannel outputChannel = Channels.newChannel(result);
try {
// copy the channels
channelCopy(inputChannel, outputChannel);
} finally {
inputChannel.close();
outputChannel.close();
}
return;
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
String error = "Failed to execute HTTP request to target";
log.debug(error, e);
throw new TransferException(MSG_HTTP_REQUEST_FAILED, new Object[] { "sendManifest", target.toString(), e.toString() }, e);
}
} finally {
postSnapshotRequest.releaseConnection();
}
}
Aggregations