use of com.twinsoft.convertigo.engine.proxy.cache.CacheEntry in project convertigo by convertigo.
the class ProxyServletRequester method coreProcessRequest.
protected Object coreProcessRequest() throws Exception {
// The proxy converts HTML data on the fly
ParameterShuttle infoShuttle = threadParameterShuttle.get();
HttpClient httpClient = threadHttpClient.get();
HttpServletRequest request = (HttpServletRequest) inputData;
InputStream siteIn = null;
try {
try {
getUserRequest(infoShuttle, context, request);
// Loading project
if (context.projectName == null)
throw new EngineException("The project name has not been specified!");
Project currentProject;
if (Engine.isStudioMode()) {
currentProject = Engine.objectsProvider.getProject(context.projectName);
if (currentProject == null) {
throw new EngineException("No project has been opened in the Studio. A project should be opened in the Studio in order that the Convertigo engine can work.");
} else if (!currentProject.getName().equalsIgnoreCase(context.projectName)) {
throw new EngineException("The requested project (\"" + context.projectName + "\") does not match with the opened project (\"" + currentProject.getName() + "\") in the Studio.\nYou cannot make a request on a different project than the one opened in the Studio.");
}
Engine.logEngine.debug("Using project from Studio");
context.project = currentProject;
} else {
if ((context.project == null) || (context.isNewSession)) {
Engine.logEngine.debug("New project requested: '" + context.projectName + "'");
context.project = Engine.theApp.databaseObjectsManager.getProjectByName(context.projectName);
Engine.logEngine.debug("Project loaded: " + context.project.getName());
}
}
// Loading sequence
if (context.sequenceName != null) {
context.requestedObject = context.project.getSequenceByName(context.sequenceName);
Engine.logEngine.debug("Loaded sequence: " + context.requestedObject.getName());
}
// Loading connector
context.loadConnector();
if (context.requestedObject != null)
context.requestedObject.context = context;
if (context.getConnector() != null)
context.getConnector().context = context;
if (Boolean.parseBoolean(EnginePropertiesManager.getProperty(PropertyName.SSL_DEBUG))) {
System.setProperty("javax.net.debug", "all");
Engine.logEngine.trace("(ProxyServletRequester) Enabling SSL debug mode");
} else {
System.setProperty("javax.net.debug", "");
Engine.logEngine.debug("(ProxyServletRequester) Disabling SSL debug mode");
}
if (context.getConnector().isTasAuthenticationRequired() && (context.tasSessionKey == null)) {
throw new EngineException("A Carioca authentication is required in order to process the transaction.");
}
infoShuttle.userID = context.tasUserName;
infoShuttle.userIP = context.remoteAddr;
// gather user id, parameters, headers from request
gatherRequestInfo(infoShuttle, request);
String t = context.statistics.start(EngineStatistics.APPLY_USER_REQUEST);
try {
// get connected
httpClient.connect(infoShuttle);
} finally {
context.statistics.stop(t);
}
if (infoShuttle.siteInputStream == null) {
Engine.logEngine.debug("(ProxyServletRequester) No input stream!");
return null;
}
siteIn = infoShuttle.siteInputStream;
Engine.logEngine.debug("(ProxyServletRequester) Start of document retransmission");
Object result;
ProxyHttpConnector proxyHttpConnector = (ProxyHttpConnector) infoShuttle.context.getConnector();
String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
String newBaseUrl = host + request.getRequestURI() + "?" + Parameter.Connector.getName() + "=" + proxyHttpConnector.getName() + "&" + Parameter.ProxyGoto.getName() + "=";
String newBaseUrlThen = newBaseUrl + URLEncoder.encode(infoShuttle.siteURL.getPath(), "UTF-8") + "&" + Parameter.ProxyThen.getName() + "=";
if (isDynamicContent(infoShuttle.siteURL.getPath(), proxyHttpConnector.getDynamicContentFiles())) {
Engine.logEngine.debug("(ProxyServletRequester) Dynamic content");
String sResponse;
StringBuffer sbResponse = new StringBuffer("");
t = context.statistics.start(EngineStatistics.APPLY_USER_REQUEST);
try {
int c = siteIn.read();
char cc;
while (c > -1) {
cc = (char) c;
sbResponse.append(cc);
c = siteIn.read();
}
} finally {
context.statistics.stop(t, true);
}
sResponse = sbResponse.toString();
result = sResponse;
if (infoShuttle.siteContentType == null) {
Engine.logEngine.warn("(ProxyServletRequester) Aborting string replacements because of null mimetype! Resource: " + infoShuttle.siteURL);
} else {
Engine.logEngine.debug("(ProxyServletRequester) String replacements");
// Text/html replacements
Engine.logEngine.debug("(ProxyServletRequester) Replacements for mime type '" + infoShuttle.siteContentType + "'");
Replacements replacements = proxyHttpConnector.getReplacementsForMimeType(infoShuttle.siteContentType);
if (!replacements.isEmpty()) {
StringEx sxResponse = new StringEx(sResponse);
StringEx sx;
String strSearched, strReplacing;
for (int i = 0; i < replacements.strReplacing.length; i++) {
strSearched = replacements.strSearched[i];
sx = new StringEx(strSearched);
sx.replaceAll("{tab}", "\t");
sx.replaceAll("{apos0x92}", "" + (char) 146);
sx.replaceAll("{newBaseUrl}", newBaseUrl);
sx.replaceAll("{newBaseUrlThen}", newBaseUrlThen);
strSearched = sx.toString();
replacements.strSearched[i] = strSearched;
Engine.logEngine.debug("(ProxyServletRequester) Replacing: " + strSearched);
strReplacing = replacements.strReplacing[i];
sx = new StringEx(strReplacing);
sx.replaceAll("{newBaseUrl}", newBaseUrl);
sx.replaceAll("{newBaseUrlThen}", newBaseUrlThen);
strReplacing = sx.toString();
replacements.strReplacing[i] = strReplacing;
Engine.logEngine.debug("(ProxyServletRequester) By: " + strReplacing);
}
Engine.logEngine.debug("(ProxyServletRequester) Replacements in progress");
sxResponse.replaceAll(replacements.strSearched, replacements.strReplacing);
Engine.logEngine.debug("(ProxyServletRequester) Replacements done!");
if (Engine.isStudioMode()) {
sxResponse.replaceAll("?" + Parameter.Connector.getName() + "=", "?" + Parameter.Context.getName() + "=" + infoShuttle.context.name + "&" + Parameter.Connector.getName() + "=");
}
result = sxResponse.toString();
}
}
infoShuttle.siteContentSize = ((String) result).length();
Engine.logEngine.debug("(ProxyServletRequester) HTML data retrieved!");
String billingClassName = context.getConnector().getBillingClassName();
if (billingClassName != null) {
try {
Engine.logContext.debug("Billing class name required: " + billingClassName);
AbstractBiller biller = (AbstractBiller) Class.forName(billingClassName).getConstructor().newInstance();
Engine.logContext.debug("Executing the biller");
biller.insertBilling(context);
} catch (Throwable e) {
Engine.logContext.warn("Unable to execute the biller (the billing is thus ignored): [" + e.getClass().getName() + "] " + e.getMessage());
}
}
} else {
Engine.logEngine.debug("(ProxyServletRequester) Static content: " + infoShuttle.siteContentType);
ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
t = context.statistics.start(EngineStatistics.APPLY_USER_REQUEST);
try {
// Read either from the cache, either from the remote server
int c = siteIn.read();
while (c > -1) {
baos.write(c);
c = siteIn.read();
}
} finally {
context.statistics.stop(t, true);
}
result = baos.toByteArray();
Engine.logEngine.debug("(ProxyServletRequester) Static data retrieved!");
// Determine if the resource has already been cached or not
String resourceUrl = infoShuttle.siteURL.toString();
CacheEntry cacheEntry = ProxyServletRequester.proxyCacheManager.getCacheEntry(resourceUrl);
if (cacheEntry == null) {
// Managing text replacements
Engine.logEngine.debug("(ProxyServletRequester) Replacements for mime type '" + infoShuttle.siteContentType + "'");
Replacements replacements = proxyHttpConnector.getReplacementsForMimeType(infoShuttle.siteContentType);
if (!replacements.isEmpty()) {
String sResult = new String((byte[]) result);
StringEx sxResponse = new StringEx(sResult);
StringEx sx;
String strSearched, strReplacing;
for (int i = 0; i < replacements.strReplacing.length; i++) {
strSearched = replacements.strSearched[i];
sx = new StringEx(strSearched);
sx.replaceAll("{tab}", "\t");
sx.replaceAll("{newBaseUrl}", newBaseUrl);
sx.replaceAll("{newBaseUrlThen}", newBaseUrlThen);
strSearched = sx.toString();
replacements.strSearched[i] = strSearched;
Engine.logEngine.debug("(ProxyServletRequester) Replacing: " + strSearched);
strReplacing = replacements.strReplacing[i];
sx = new StringEx(strReplacing);
sx.replaceAll("{newBaseUrl}", newBaseUrl);
sx.replaceAll("{newBaseUrlThen}", newBaseUrlThen);
strReplacing = sx.toString();
replacements.strReplacing[i] = strReplacing;
Engine.logEngine.debug("(ProxyServletRequester) By: " + strReplacing);
}
Engine.logEngine.debug("(ProxyServletRequester) Replacements in progress");
sxResponse.replaceAll(replacements.strSearched, replacements.strReplacing);
Engine.logEngine.debug("(ProxyServletRequester) Replacements done!");
result = sxResponse.toString().getBytes();
}
if (infoShuttle.httpCode == 200) {
Engine.logEngine.debug("(ProxyServletRequester) Resource stored: " + resourceUrl);
cacheEntry = proxyCacheManager.storeResponse(resourceUrl, (byte[]) result);
cacheEntry.contentLength = ((byte[]) result).length;
infoShuttle.siteContentSize = cacheEntry.contentLength;
cacheEntry.contentType = infoShuttle.siteContentType;
}
} else {
infoShuttle.httpCode = 200;
infoShuttle.siteContentSize = cacheEntry.contentLength;
infoShuttle.siteContentType = cacheEntry.contentType;
}
baos.close();
}
Engine.logEngine.debug("(ProxyServletRequester) End of document retransmission");
return result;
} finally {
if (siteIn != null) {
try {
siteIn.close();
} catch (Exception e) {
}
}
}
} finally {
context.contentType = infoShuttle.siteContentType;
httpClient.disconnect();
}
}
use of com.twinsoft.convertigo.engine.proxy.cache.CacheEntry in project convertigo by convertigo.
the class WebClippingServletRequester method coreProcessRequest.
@Override
protected Object coreProcessRequest() throws Exception {
try {
if (context.getConnector() != null) {
if (context.getConnector() instanceof HtmlConnector) {
HtmlConnector connector = (HtmlConnector) context.getConnector();
if (bDirty) {
return Boolean.toString(connector.getHtmlParser().getDomDirty(context)).getBytes();
} else {
HttpServletRequest request = (HttpServletRequest) inputData;
String webURL = request.getQueryString();
String contentType = null;
if (webURL == null) {
Engine.logEngine.debug("(WebClippingServletRequester) no webURL !!! return empty result");
return new byte[0];
}
// TODO: q&d fix #419 [Invalid URI error if ressource image to get contains "\"] ; may be include it in httpClient ?
webURL = webURL.replace('\\', '/');
Engine.logEngine.trace("(WebClippingServletRequester) weburl is " + webURL);
InputStream is = bCache ? cache.getResource(webURL) : null;
if (is == null) {
String charset = null;
byte[] data = null;
synchronized (context) {
connector.sUrl = webURL;
if (context.requestedObject == null) {
context.requestedObject = connector.getDefaultTransaction();
context.transaction = (HtmlTransaction) context.requestedObject;
context.requestedObject.createRequestableThread();
}
// save old context headers
String exContentType = context.contentType;
Header[] exHeaders = context.getResponseHeaders();
data = connector.getData(context);
contentType = context.contentType;
charset = connector.getCharset();
// restore old context headers
context.contentType = exContentType;
context.setResponseHeaders(exHeaders);
Engine.logEngine.trace("(WebClippingServletRequester) contentType is " + contentType);
Engine.logEngine.trace("(WebClippingServletRequester) charset is " + charset);
}
request.setAttribute("convertigo.charset", charset);
data = rewriteCSS(data, webURL, contentType, charset);
is = new ByteArrayInputStream(data);
if (bCache) {
CacheEntry c = cache.storeResponse(webURL, data);
c.contentLength = data.length;
c.contentType = contentType;
}
} else {
contentType = cache.getCacheEntry(webURL).contentType;
}
request.setAttribute("convertigo.contentType", contentType);
byte[] data = new byte[is.available()];
is.read(data);
is.close();
return data;
}
} else if (context.getConnector() instanceof JavelinConnector && bDirty) {
Session session = Engine.theApp.sessionManager.getSession(context.contextID);
if (session != null) {
return Boolean.toString(session.isSomethingChange());
}
}
}
} catch (EngineException e) {
Engine.logEngine.error("Error", e);
}
return null;
}
use of com.twinsoft.convertigo.engine.proxy.cache.CacheEntry in project convertigo by convertigo.
the class HttpClient method connect.
public void connect(ParameterShuttle infoShuttle) throws Exception {
boolean noEncoding = false;
boolean zipEncoding = false;
boolean doConvert = false;
HttpConnector connector = (HttpConnector) infoShuttle.context.getConnector();
// filter/convert http headers before they are outgoing to remote site
HttpBridge.convertIncomingRequest(infoShuttle);
// create HttpURLConnection
Engine.logEngine.debug("(HttpClient) Site URL: " + infoShuttle.siteURL);
String resourceUrl = infoShuttle.siteURL.toString();
infoShuttle.siteInputStream = ProxyServletRequester.proxyCacheManager.getResource(resourceUrl);
if (infoShuttle.siteInputStream == null) {
Engine.logEngine.debug("(HttpClient) Resource requested: " + resourceUrl);
// Getting result
Engine.logEngine.debug("(HttpClient) Getting response");
byte[] result = getData(connector, resourceUrl, infoShuttle);
Engine.logEngine.trace("(HttpClient) Data received:\n" + new String(result));
// get connected and get info from HTTP headers and save them into ParameterShuttle
infoShuttle.httpCode = method.getStatusCode();
Engine.logEngine.debug("(HttpClient) Response code: " + infoShuttle.httpCode);
Header contentLength = method.getResponseHeader("Content-Length");
if (contentLength != null)
infoShuttle.siteContentSize = Integer.parseInt(contentLength.getValue());
Header contentType = method.getResponseHeader("Content-Type");
if (contentType != null)
infoShuttle.siteContentType = contentType.getValue();
Engine.logEngine.debug("(HttpClient) Content type: " + infoShuttle.siteContentType);
infoShuttle.siteContentHTML = infoShuttle.siteContentType != null && infoShuttle.siteContentType.toLowerCase().startsWith("text/html");
if (infoShuttle.siteContentHTML) {
noEncoding = true;
Header encodingHeader = method.getResponseHeader("Content-Encoding");
if (encodingHeader != null) {
String contentEncoding = encodingHeader.getValue();
if (contentEncoding == null || contentEncoding.length() == 0)
noEncoding = true;
else if (contentEncoding.toLowerCase().equals("gzip"))
zipEncoding = true;
}
doConvert = noEncoding || zipEncoding;
}
// filter/convert http headers before they are outgoing to user
Engine.logEngine.debug("(HttpClient) Outcoming HTTP headers:");
Header[] headers = method.getResponseHeaders();
Header header;
String value, key;
for (int index = 0; index < headers.length; index++) {
header = headers[index];
value = header.getValue();
key = header.getName();
if (key == null)
continue;
infoShuttle.siteHeaderNames.add(key.toLowerCase());
infoShuttle.siteHeaderValues.add(value);
Engine.logEngine.debug(key.toLowerCase() + "=" + value);
}
HttpBridge.convertOutgoingHeaders(infoShuttle, !doConvert);
try {
infoShuttle.siteInputStream = method.getResponseBodyAsStream();
} catch (Exception e) {
return;
}
Engine.logEngine.debug("(HttpClient) Connection opened");
} else {
Engine.logEngine.debug("(HttpClient) Resource returned from the cache: " + resourceUrl);
CacheEntry cacheEntry = ProxyServletRequester.proxyCacheManager.getCacheEntry(resourceUrl);
infoShuttle.siteContentSize = cacheEntry.contentLength;
infoShuttle.siteContentType = cacheEntry.contentType;
infoShuttle.siteContentHTML = false;
}
// choose right InputStream to output to browser
if (doConvert) {
if (zipEncoding) {
infoShuttle.siteInputStream = htmlInputStream.open(infoShuttle, new GZIPInputStream(infoShuttle.siteInputStream), infoShuttle.siteURL);
} else if (noEncoding) {
infoShuttle.siteInputStream = htmlInputStream.open(infoShuttle, infoShuttle.siteInputStream, infoShuttle.siteURL);
}
}
}
Aggregations