use of com.dexels.navajo.script.api.ClientInfo in project navajo by Dexels.
the class TmlHttpServlet method doPost.
/**
* Handle a request.
*
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
MDC.clear();
Date created = new java.util.Date();
long start = created.getTime();
String sendEncoding = request.getHeader("Accept-Encoding");
String recvEncoding = request.getHeader("Content-Encoding");
if (sendEncoding != null) {
MDC.put("Accept-Encoding", sendEncoding);
}
if (recvEncoding != null) {
MDC.put("Content-Encoding", recvEncoding);
}
DispatcherInterface dis = null;
BufferedReader r = null;
BufferedWriter out = null;
try {
Navajo in = null;
if (streamingMode) {
if (sendEncoding != null && sendEncoding.equals(COMPRESS_JZLIB)) {
r = new BufferedReader(new java.io.InputStreamReader(new InflaterInputStream(request.getInputStream()), "UTF-8"));
} else if (sendEncoding != null && sendEncoding.equals(COMPRESS_GZIP)) {
r = new BufferedReader(new java.io.InputStreamReader(new java.util.zip.GZIPInputStream(request.getInputStream()), "UTF-8"));
} else {
r = new BufferedReader(request.getReader());
}
in = NavajoFactory.getInstance().createNavajo(r);
r.close();
r = null;
} else {
logger.info("Warning: Using non-streaming mode for " + request.getRequestURI() + ", file written: " + logfileIndex + ", total size: " + bytesWritten);
InputStream is = request.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
copyResource(bos, is);
is.close();
bos.close();
byte[] bytes = bos.toByteArray();
try {
if (sendEncoding != null && sendEncoding.equals(COMPRESS_JZLIB)) {
r = new BufferedReader(new java.io.InputStreamReader(new InflaterInputStream(new ByteArrayInputStream(bytes)), "UTF-8"));
} else if (sendEncoding != null && sendEncoding.equals(COMPRESS_GZIP)) {
r = new BufferedReader(new java.io.InputStreamReader(new java.util.zip.GZIPInputStream(new ByteArrayInputStream(bytes)), "UTF-8"));
} else {
r = new BufferedReader(new java.io.InputStreamReader(new ByteArrayInputStream(bytes), "UTF-8"));
}
in = NavajoFactory.getInstance().createNavajo(r);
if (in == null) {
throw new Exception("Invalid Navajo");
}
r.close();
r = null;
} catch (Throwable t) {
// Write request to temp file.
File f = DispatcherFactory.getInstance().getTempDir();
if (f != null) {
bytesWritten += bytes.length;
logfileIndex++;
FileOutputStream fos = new FileOutputStream(new File(f, "request-" + logfileIndex));
copyResource(fos, new ByteArrayInputStream(bytes));
fos.close();
PrintWriter fw = new PrintWriter(new FileWriter(new File(f, "exception-" + logfileIndex)));
t.printStackTrace(fw);
fw.flush();
fw.close();
}
dumHttp(request, logfileIndex, f);
throw new ServletException(t);
}
}
long stamp = System.currentTimeMillis();
int pT = (int) (stamp - start);
if (in == null) {
throw new ServletException("Invalid request.");
}
Header header = in.getHeader();
if (header == null) {
throw new ServletException("Empty Navajo header.");
}
dis = DispatcherFactory.getInstance();
if (dis == null) {
System.err.println("SERIOUS: No dispatcher found. The navajo context did not initialize properly, check the logs to find out why!");
return;
}
// Check for certificate.
Object certObject = request.getAttribute("javax.servlet.request.X509Certificate");
// Call Dispatcher with parsed TML document as argument.
String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.equals("")) {
ip = request.getRemoteAddr();
}
ClientInfo clientInfo = new ClientInfo(ip, "unknown", recvEncoding, pT, (recvEncoding != null && (recvEncoding.equals(COMPRESS_GZIP) || recvEncoding.equals(COMPRESS_JZLIB))), (sendEncoding != null && (sendEncoding.equals(COMPRESS_GZIP) || sendEncoding.equals(COMPRESS_JZLIB))), request.getContentLength(), created);
String instance = determineInstanceFromRequest(request);
Navajo outDoc = handleTransaction(instance, dis, in, certObject, clientInfo);
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Accept-Ranges", "none");
if (recvEncoding != null && recvEncoding.equals(COMPRESS_JZLIB)) {
response.setHeader("Content-Encoding", COMPRESS_JZLIB);
out = new BufferedWriter(new OutputStreamWriter(new DeflaterOutputStream(response.getOutputStream()), "UTF-8"));
} else if (recvEncoding != null && recvEncoding.equals(COMPRESS_GZIP)) {
response.setHeader("Content-Encoding", COMPRESS_GZIP);
out = new BufferedWriter(new OutputStreamWriter(new java.util.zip.GZIPOutputStream(response.getOutputStream()), "UTF-8"));
} else {
out = new BufferedWriter(response.getWriter());
}
outDoc.write(out);
out.flush();
out.close();
if (in.getHeader() != null && outDoc.getHeader() != null && !Dispatcher.isSpecialwebservice(in.getHeader().getRPCName())) {
statLogger.info("Finished {} ({}) in {}ms", outDoc.getHeader().getHeaderAttribute("accessId"), in.getHeader().getRPCName(), (System.currentTimeMillis() - start));
}
out = null;
} catch (Throwable e) {
logger.error("Error: ", e);
dumHttp(request, -1, null);
if (e instanceof FatalException) {
FatalException fe = (FatalException) e;
if (fe.getMessage().equals("500.13")) {
// Server too busy.
throw new ServletException("500.13", e);
}
}
throw new ServletException(e);
} finally {
dis = null;
if (r != null) {
try {
r.close();
} catch (Exception e) {
// NOT INTERESTED.
}
}
if (out != null) {
try {
out.close();
} catch (Exception e) {
// NOT INTERESTED.
}
}
}
}
use of com.dexels.navajo.script.api.ClientInfo in project navajo by Dexels.
the class BaseServiceRunner method execute.
/**
* Handle a request.
*
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private final void execute() throws IOException, ServletException {
// BufferedReader r = null;
String instance = myRequest.getInstance();
if (instance != null) {
MDC.put("instance", instance);
myRequest.getInputDocument().getHeader().setHeaderAttribute("instance", instance);
}
try {
Navajo in = getInputNavajo();
in.getHeader().setHeaderAttribute("useComet", "true");
if (in.getHeader().getHeaderAttribute("callback") != null) {
String callback = in.getHeader().getHeaderAttribute("callback");
try {
Navajo callbackNavajo = getLocalClient().handleCallback(getNavajoInstance(), in, callback);
writeOutput(in, callbackNavajo);
} finally {
endTransaction();
}
} else {
boolean continuationFound = false;
try {
String queueId = myQueue.getId();
int queueLength = myQueue.getQueueSize();
ClientInfo clientInfo = getRequest().createClientInfo(scheduledAt, startedAt, queueLength, queueId);
Navajo outDoc = getLocalClient().handleInternal(getNavajoInstance(), in, getRequest().getCert(), clientInfo);
// Do do: Support async services in a more elegant way.
if (!isAborted()) {
writeOutput(in, outDoc);
} else {
logger.warn("Aborted: Can't write output!");
}
} finally {
if (!continuationFound) {
// this will be the responsibility of the next thread,
// who will finish the continuation.
endTransaction();
}
}
}
} catch (Throwable e) {
if (e instanceof FatalException) {
FatalException fe = (FatalException) e;
if (fe.getMessage().equals("500.13")) {
// Server too busy.
throw new ServletException("500.13");
}
}
throw new ServletException(e);
} finally {
MDC.remove("instance");
}
}
use of com.dexels.navajo.script.api.ClientInfo in project navajo by Dexels.
the class BaseRequestImpl method createClientInfo.
@Override
public ClientInfo createClientInfo(long scheduledAt, long startedAt, int queueLength, String queueId) {
String ip = getIpAddress();
ClientInfo clientInfo = new ClientInfo(ip, "unknown", contentEncoding, (int) (scheduledAt - connectedAt), (int) (startedAt - scheduledAt), queueLength, queueId, (contentEncoding != null && (contentEncoding.equals(COMPRESS_GZIP) || contentEncoding.equals(COMPRESS_JZLIB))), (acceptEncoding != null && (acceptEncoding.equals(COMPRESS_GZIP) || acceptEncoding.equals(COMPRESS_JZLIB))), request.getContentLength(), new java.util.Date(connectedAt));
clientInfo.setAuthHeader(getAuthHeader());
return clientInfo;
}
use of com.dexels.navajo.script.api.ClientInfo in project navajo by Dexels.
the class TmlContinuationRunner method execute.
/**
* Handle a request.
*
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private final void execute() throws IOException, ServletException {
startedAt = System.currentTimeMillis();
setCommitted(true);
// BufferedReader r = null;
try {
Navajo in = getInputNavajo();
in.getHeader().setHeaderAttribute("useComet", "true");
boolean continuationFound = false;
try {
int queueSize = getRequestQueue().getQueueSize();
String queueId = getRequestQueue().getId();
ClientInfo clientInfo = getRequest().createClientInfo(scheduledAt, startedAt, queueSize, queueId);
setResponseNavajo(getLocalClient().handleInternal(getNavajoInstance(), in, getRequest().getCert(), clientInfo));
} finally {
if (!continuationFound) {
// resumeContinuation();
// continuation.complete();
endTransaction();
}
}
// }
} catch (Throwable e) {
// e.printStackTrace(System.err);
if (e instanceof FatalException) {
FatalException fe = (FatalException) e;
if (fe.getMessage().equals("500.13")) {
// Server too busy.
continuation.undispatch();
throw new ServletException("500.13");
}
}
throw new ServletException(e);
} finally {
MDC.clear();
}
}
Aggregations