Search in sources :

Example 11 with Header

use of com.dexels.navajo.document.Header 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.
            }
        }
    }
}
Also used : FileWriter(java.io.FileWriter) BufferedWriter(java.io.BufferedWriter) ServletException(javax.servlet.ServletException) DeflaterOutputStream(com.jcraft.jzlib.DeflaterOutputStream) PrintWriter(java.io.PrintWriter) FatalException(com.dexels.navajo.script.api.FatalException) InflaterInputStream(com.jcraft.jzlib.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(com.jcraft.jzlib.InflaterInputStream) InputStream(java.io.InputStream) Navajo(com.dexels.navajo.document.Navajo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) FatalException(com.dexels.navajo.script.api.FatalException) ServletException(javax.servlet.ServletException) NavajoException(com.dexels.navajo.document.NavajoException) IOException(java.io.IOException) Header(com.dexels.navajo.document.Header) DispatcherInterface(com.dexels.navajo.server.DispatcherInterface) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) ClientInfo(com.dexels.navajo.script.api.ClientInfo) File(java.io.File)

Example 12 with Header

use of com.dexels.navajo.document.Header in project navajo by Dexels.

the class GetHeader method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    String result;
    if (inMessage != null) {
        Header h = inMessage.getHeader();
        result = h.getHeaderAttribute(getStringOperand(0));
    } else {
        return "empty header";
    }
    return result;
}
Also used : Header(com.dexels.navajo.document.Header)

Example 13 with Header

use of com.dexels.navajo.document.Header in project navajo by Dexels.

the class NavajoMap method run.

@Override
public void run() {
    try {
        Header h = outDoc.getHeader();
        if (h == null) {
            h = NavajoFactory.getInstance().createHeader(outDoc, method, username, password, -1);
            outDoc.addHeader(h);
        } else {
            h.setRPCName(method);
            h.setRPCPassword(password);
            h.setRPCUser(username);
        }
        // Clear request id.
        h.setRequestId(null);
        h.setHeaderAttribute("parentaccessid", access.accessID);
        h.setHeaderAttribute("application", access.getApplication());
        h.setHeaderAttribute("organization", access.getOrganization());
        if (locale != null && !locale.equals("")) {
            h.setHeaderAttribute("locale", locale);
        } else if (access.getInDoc() != null && access.getInDoc().getHeader().getHeaderAttribute("locale") != null) {
            h.setHeaderAttribute("locale", access.getInDoc().getHeader().getHeaderAttribute("locale"));
        }
        if (userAgent != null && !userAgent.equals("")) {
            h.setHeaderAttribute("user_agent", userAgent);
        } else if (access.getInDoc() != null && access.getInDoc().getHeader().getHeaderAttribute("user_agent") != null) {
            h.setHeaderAttribute("user_agent", access.getInDoc().getHeader().getHeaderAttribute("user_agent"));
        }
        // TODO: MAYBE ALL?
        String tenant = access.getTenant();
        boolean skipAuth = true;
        if (this.tenant != null && !this.tenant.equals("")) {
            tenant = this.tenant;
        }
        inDoc = DispatcherFactory.getInstance().handle(outDoc, tenant, skipAuth);
        checkSetPerformOrderBy();
        serviceFinished = true;
        serviceCalled = true;
        continueAfterRun();
    } catch (Exception e) {
        setException(e);
    } finally {
        serviceFinished = true;
        serviceCalled = true;
    }
}
Also used : Header(com.dexels.navajo.document.Header) NavajoException(com.dexels.navajo.document.NavajoException) AuthorizationException(com.dexels.navajo.script.api.AuthorizationException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) IOException(java.io.IOException) SystemException(com.dexels.navajo.script.api.SystemException) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException)

Example 14 with Header

use of com.dexels.navajo.document.Header in project navajo by Dexels.

the class AsyncProxyMap method run.

@Override
public void run() throws UserException {
    Header h = outDoc.getHeader();
    if (h == null) {
        h = NavajoFactory.getInstance().createHeader(outDoc, method, username, password, -1);
        outDoc.addHeader(h);
    } else {
        h.setRPCName(method);
        h.setRPCPassword(password);
        h.setRPCUser(username);
    }
    // Clear request id.
    h.setRequestId(null);
    try {
        inDoc = DispatcherFactory.getInstance().handle(outDoc);
    } catch (Exception e) {
        throw new UserException(-1, e.getMessage(), e);
    } finally {
        logger.info("Setting set is finished.");
        setIsFinished();
    }
}
Also used : Header(com.dexels.navajo.document.Header) UserException(com.dexels.navajo.script.api.UserException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Example 15 with Header

use of com.dexels.navajo.document.Header in project navajo by Dexels.

the class Dispatcher method generateAuthorizationErrorMessage.

/**
 * Generate a Navajo authorization error response.
 *
 * @param access
 *            Beware, might be null
 * @param ae
 * @return
 * @throws FatalException
 */
private final Navajo generateAuthorizationErrorMessage(Access access, AuthorizationException ae, String rpcName) throws FatalException {
    try {
        Navajo outMessage = NavajoFactory.getInstance().createNavajo();
        // Make sure empty Header is constructed
        Header h = NavajoFactory.getInstance().createHeader(outMessage, "", "", "", -1);
        outMessage.addHeader(h);
        Message errorMessage = NavajoFactory.getInstance().createMessage(outMessage, (ae.isNotAuthorized() ? AuthorizationException.AUTHORIZATION_ERROR_MESSAGE : AuthorizationException.AUTHENTICATION_ERROR_MESSAGE));
        outMessage.addMessage(errorMessage);
        Property prop = NavajoFactory.getInstance().createProperty(outMessage, "Message", Property.STRING_PROPERTY, ae.getMessage(), 0, "Message", Property.DIR_OUT);
        errorMessage.addProperty(prop);
        prop = NavajoFactory.getInstance().createProperty(outMessage, "User", Property.STRING_PROPERTY, ae.getUser(), 0, "User", Property.DIR_OUT);
        errorMessage.addProperty(prop);
        prop = NavajoFactory.getInstance().createProperty(outMessage, "Webservice", Property.STRING_PROPERTY, rpcName, 0, "User", Property.DIR_OUT);
        errorMessage.addProperty(prop);
        if (access != null) {
            access.setException(ae);
            access.setOutputDoc(outMessage);
        }
        return outMessage;
    } catch (Exception e) {
        throw new FatalException(e.getMessage(), e);
    }
}
Also used : Header(com.dexels.navajo.document.Header) Message(com.dexels.navajo.document.Message) FatalException(com.dexels.navajo.script.api.FatalException) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) FatalException(com.dexels.navajo.script.api.FatalException) NavajoException(com.dexels.navajo.document.NavajoException) AuthorizationException(com.dexels.navajo.script.api.AuthorizationException) UserException(com.dexels.navajo.script.api.UserException) TriggerException(com.dexels.navajo.server.enterprise.scheduler.TriggerException) IOException(java.io.IOException) SystemException(com.dexels.navajo.script.api.SystemException)

Aggregations

Header (com.dexels.navajo.document.Header)29 Navajo (com.dexels.navajo.document.Navajo)19 NavajoException (com.dexels.navajo.document.NavajoException)9 IOException (java.io.IOException)9 FatalException (com.dexels.navajo.script.api.FatalException)7 UserException (com.dexels.navajo.script.api.UserException)7 Message (com.dexels.navajo.document.Message)6 SystemException (com.dexels.navajo.script.api.SystemException)6 AuthorizationException (com.dexels.navajo.script.api.AuthorizationException)5 Property (com.dexels.navajo.document.Property)4 TriggerException (com.dexels.navajo.server.enterprise.scheduler.TriggerException)4 ClientException (com.dexels.navajo.client.ClientException)3 Access (com.dexels.navajo.script.api.Access)3 NavajoResponseHandler (com.dexels.navajo.client.NavajoResponseHandler)2 MappableException (com.dexels.navajo.script.api.MappableException)2 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2