use of com.twinsoft.convertigo.beans.transactions.DownloadHttpTransaction 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();
}
}
Aggregations