use of com.twinsoft.convertigo.engine.util.BigMimeMultipart in project convertigo by convertigo.
the class HttpConnector method executeMethod.
private byte[] executeMethod(HttpMethod method, final Context context) throws IOException, URIException, MalformedURLException, EngineException {
Header[] requestHeaders, responseHeaders = null;
byte[] result = null;
String contents = null;
int statuscode = -1;
final String referer = this.referer;
if (!context.requestedObject.runningThread.bContinue)
return null;
Engine.logBeans.debug("(HttpConnector) Executing method - " + method.getName() + "(" + method.getPath() + ")");
try {
AbstractHttpTransaction transaction = (AbstractHttpTransaction) context.requestedObject;
requestHeaders = method.getRequestHeaders();
if (Engine.logBeans.isTraceEnabled())
Engine.logBeans.trace("(HttpConnector) Request headers :\n" + Arrays.asList(requestHeaders).toString());
statuscode = doExecuteMethod(method, context);
Engine.logBeans.debug("(HttpConnector) Status: " + method.getStatusLine().toString());
responseHeaders = method.getResponseHeaders();
context.setResponseHeaders(responseHeaders);
if (Engine.logBeans.isTraceEnabled())
Engine.logBeans.trace("(HttpConnector) Response headers:\n" + Arrays.asList(responseHeaders).toString());
if (statuscode != -1) {
InputStream in = method.getResponseBodyAsStream();
if (in != null) {
/**
* Retrieve response charset if available in responseHeaders
*/
charset = null;
// add GZip support #320
boolean checkGZip = false;
for (int i = 0; i < responseHeaders.length && (charset == null || !checkGZip); i++) {
Header head = responseHeaders[i];
if (HeaderName.ContentType.is(head)) {
context.contentType = head.getValue();
HeaderElement[] els = head.getElements();
for (int j = 0; j < els.length && charset == null; j++) {
NameValuePair nvp = els[j].getParameterByName("charset");
if (nvp != null)
charset = nvp.getValue();
}
} else if (HeaderName.ContentEncoding.is(head)) {
checkGZip = true;
HeaderElement[] els = head.getElements();
for (int j = 0; j < els.length; j++) if ("gzip".equals(els[j].getName())) {
Engine.logBeans.debug("(HttpConnector) Decode GZip stream");
in = new GZIPInputStream(in);
}
}
}
if (context.contentType != null && context.contentType.startsWith("multipart/") && context.requestedObject instanceof AbstractHttpTransaction) {
Engine.logBeans.debug("(HttpConnector) Decoding multipart contentType");
try {
BigMimeMultipart mp = new BigMimeMultipart(new BufferedInputStream(in), context.contentType);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
mp.nextPart(bos);
result = bos.toByteArray();
if (transaction.getAllowDownloadAttachment()) {
Document doc = context.outputDocument;
Element attInfo = null;
File file = File.createTempFile("c8o_", ".part");
for (MimePart bp = mp.nextPart(file); bp != null; bp = mp.nextPart(file)) {
try {
file.deleteOnExit();
if (attInfo == null) {
Engine.logBeans.debug("(HttpConnector) Saving attachment(s)");
attInfo = doc.createElement("AttachmentInfo");
doc.getDocumentElement().appendChild(attInfo);
}
Element att = doc.createElement("attachment");
attInfo.appendChild(att);
String cid = bp.getContentID();
if (cid != null) {
cid = cid.replaceFirst("^<?(.*?)>?$", "$1");
att.setAttribute("cid", "cid:" + cid);
}
Engine.logBeans.debug("(HttpConnector) Saving the attachment cid: " + cid + " in file: " + file.getAbsolutePath());
att.setAttribute("filepath", file.getAbsolutePath());
Enumeration<javax.mail.Header> headers = GenericUtils.cast(bp.getAllHeaders());
while (headers.hasMoreElements()) {
javax.mail.Header header = headers.nextElement();
Element eHeader = doc.createElement("header");
att.appendChild(eHeader);
eHeader.setAttribute("name", header.getName());
eHeader.setAttribute("value", header.getValue());
}
} catch (Exception e1) {
Engine.logBeans.error("(HttpConnector) Failed to retrieve the attachment in " + file.getAbsolutePath(), e1);
}
file = File.createTempFile("c8o_", ".part");
}
file.delete();
in.close();
}
} catch (Exception e) {
Engine.logBeans.error("(HttpConnector) Failed to retrieve attachments", e);
}
} else {
result = IOUtils.toByteArray(in);
in.close();
}
}
if (Engine.logBeans.isTraceEnabled()) {
contents = new String((result != null) ? result : new byte[] {});
Engine.logBeans.trace("(HttpConnector) Response content:\n" + contents);
}
String redirectUrl, newuri;
// Handles REDIRECTION through Location header
if (transaction.isFollowRedirect() && (statuscode == HttpStatus.SC_MOVED_TEMPORARILY || statuscode == HttpStatus.SC_MOVED_PERMANENTLY || statuscode == HttpStatus.SC_SEE_OTHER || statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header location = method.getResponseHeader("Location");
if (location != null) {
newuri = location.getValue();
if ((newuri == null) || (newuri.equals(""))) {
newuri = "/";
}
// ignore any data after the ";" character
int split = newuri.indexOf(';');
if (split != -1) {
newuri = newuri.substring(0, split);
}
redirectUrl = getAbsoluteUrl(method, newuri);
Engine.logBeans.debug("(HttpConnector) Redirecting to : " + redirectUrl);
result = getData(context, redirectUrl);
} else {
Engine.logBeans.debug("(HttpConnector) Invalid redirect!");
}
}
}
// Added by julienda - #3433 - 04/03/2013
AbstractHttpTransaction abstractHttpTransaction = (AbstractHttpTransaction) context.transaction;
if (abstractHttpTransaction.getHttpInfo()) {
Document doc = context.outputDocument;
// Parent Element
httpInfoElement = doc.createElement(abstractHttpTransaction.getHttpInfoTagName());
// Add requested URL
Element urlElement = doc.createElement("url");
urlElement.setTextContent(referer);
httpInfoElement.appendChild(urlElement);
// Add status code
Element httpStatusElement = doc.createElement("status");
httpStatusElement.setAttribute("code", Integer.toString(statuscode));
httpStatusElement.setAttribute("text", method.getStatusText());
httpInfoElement.appendChild(httpStatusElement);
// We add headers informations
List<Header> headers = Arrays.asList(requestHeaders);
if (!headers.isEmpty()) {
Element httpHeadersElement = doc.createElement("headers");
for (int i = 0; i < headers.size(); i++) {
Element elt = doc.createElement("header");
elt.setAttribute("name", headers.get(i).getName());
elt.setAttribute("value", headers.get(i).getValue());
httpHeadersElement.appendChild(elt);
}
httpInfoElement.appendChild(httpHeadersElement);
}
// we add response header information
if (responseHeaders.length != 0) {
Element httpHeadersElement = doc.createElement("responseHeaders");
for (int i = 0; i < responseHeaders.length; i++) {
Element elt = doc.createElement("header");
elt.setAttribute("name", responseHeaders[i].getName());
elt.setAttribute("value", responseHeaders[i].getValue());
httpHeadersElement.appendChild(elt);
}
httpInfoElement.appendChild(httpHeadersElement);
}
doc.getDocumentElement().appendChild(httpInfoElement);
}
} finally {
method.releaseConnection();
}
return result;
}
Aggregations