Search in sources :

Example 1 with DispatcherInterface

use of com.dexels.navajo.server.DispatcherInterface in project navajo by Dexels.

the class MappingUtils method isObjectMappable.

public static final boolean isObjectMappable(String className) throws UserException {
    try {
        DispatcherInterface instance = DispatcherFactory.getInstance();
        Class c = null;
        if (instance == null) {
            c = Class.forName(className);
        } else {
            c = Class.forName(className, true, instance.getNavajoConfig().getClassloader());
        }
        return (Mappable.class.isAssignableFrom(c));
    } catch (Exception e) {
        throw new UserException(-1, "Could not handle class as either mappable or POJO bean: " + className + ", cause: " + e.getMessage(), e);
    }
}
Also used : Mappable(com.dexels.navajo.script.api.Mappable) DispatcherInterface(com.dexels.navajo.server.DispatcherInterface) UserException(com.dexels.navajo.script.api.UserException) MappingException(com.dexels.navajo.script.api.MappingException) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SystemException(com.dexels.navajo.script.api.SystemException)

Example 2 with DispatcherInterface

use of com.dexels.navajo.server.DispatcherInterface 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 3 with DispatcherInterface

use of com.dexels.navajo.server.DispatcherInterface in project navajo by Dexels.

the class TmlHttpServlet method callDirect.

private final void callDirect(HttpServletRequest request, HttpServletResponse response) {
    String service = request.getParameter("service");
    if (service == null) {
        logRequestParams(request);
        return;
    }
    final String instance = determineInstanceFromRequest(request);
    // PrintWriter out = response.getWriter();
    Navajo tbMessage = null;
    DispatcherInterface dis = null;
    dis = DispatcherFactory.getInstance();
    try {
        tbMessage = constructFromRequest(request);
        Navajo resultMessage = handleTransaction(instance, dis, tbMessage, null, null);
        sendResponse(request, response, resultMessage);
        statLogger.info("direct legacy call");
    } catch (Exception ce) {
        logger.error("Error: ", ce);
    } finally {
        dis = null;
    }
}
Also used : DispatcherInterface(com.dexels.navajo.server.DispatcherInterface) Navajo(com.dexels.navajo.document.Navajo) FatalException(com.dexels.navajo.script.api.FatalException) ServletException(javax.servlet.ServletException) NavajoException(com.dexels.navajo.document.NavajoException) IOException(java.io.IOException)

Example 4 with DispatcherInterface

use of com.dexels.navajo.server.DispatcherInterface in project navajo by Dexels.

the class JMXHelper method getObjectName.

private static final ObjectName getObjectName(String domain, String type) {
    if (applicationPrefix == null) {
        synchronized (semaphore) {
            DispatcherInterface instance = DispatcherFactory.getInstance();
            if (instance == null) {
                // logger.debug("Navajo instance not started. Is navajo context listener valid? Check web.xml");
                return null;
            }
            NavajoConfigInterface navajoConfig = instance.getNavajoConfig();
            if (navajoConfig == null) {
                throw new RuntimeException("Navajo instance not started. Is navajo context listener valid? Check web.xml");
            }
            applicationPrefix = navajoConfig.getInstanceName();
            if (applicationPrefix == null) {
                applicationPrefix = "unnamedapplication";
            }
        }
    }
    try {
        return new ObjectName(constructObjectName(domain) + type + ",instance=" + applicationPrefix);
    } catch (MalformedObjectNameException e) {
        logger.error("Error: ", e);
        return null;
    } catch (NullPointerException e) {
        logger.error("Error: ", e);
        return null;
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) DispatcherInterface(com.dexels.navajo.server.DispatcherInterface) NavajoConfigInterface(com.dexels.navajo.server.NavajoConfigInterface) ObjectName(javax.management.ObjectName)

Example 5 with DispatcherInterface

use of com.dexels.navajo.server.DispatcherInterface in project navajo by Dexels.

the class CallService method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    Operand result = null;
    String serviceName = getStringOperand(0);
    String expression = getOperands().size() == 1 ? null : getStringOperand(1);
    if (getNavajo() == null) {
        throw new TMLExpressionException("No Navajo Request object available.");
    }
    if (getOperands().size() > 2) {
        throw new TMLExpressionException("Invalid number of parameters.");
    }
    try {
        Navajo response = getNavajo().getNavajo(serviceName);
        if (response == null) {
            DispatcherInterface dispatcher = DispatcherFactory.getInstance();
            Navajo input = getNavajo().copy();
            input.getHeader().setRPCName(serviceName);
            response = dispatcher.handle(input, this.getAccess().getTenant(), true);
            getNavajo().addNavajo(serviceName, response);
        }
        if (expression == null) {
            Binary bbb = new Binary();
            OutputStream os = bbb.getOutputStream();
            response.write(os);
            os.flush();
            os.close();
            return bbb;
        } else {
            result = Expression.evaluate(expression, response);
        }
    } catch (Exception ex) {
        logger.error("Error: ", ex);
    }
    if (result != null) {
        return result.value;
    } else {
        return null;
    }
}
Also used : DispatcherInterface(com.dexels.navajo.server.DispatcherInterface) Operand(com.dexels.navajo.document.Operand) OutputStream(java.io.OutputStream) Navajo(com.dexels.navajo.document.Navajo) Binary(com.dexels.navajo.document.types.Binary) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Aggregations

DispatcherInterface (com.dexels.navajo.server.DispatcherInterface)5 Navajo (com.dexels.navajo.document.Navajo)3 NavajoException (com.dexels.navajo.document.NavajoException)3 FatalException (com.dexels.navajo.script.api.FatalException)2 IOException (java.io.IOException)2 ServletException (javax.servlet.ServletException)2 Header (com.dexels.navajo.document.Header)1 Operand (com.dexels.navajo.document.Operand)1 Binary (com.dexels.navajo.document.types.Binary)1 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)1 ClientInfo (com.dexels.navajo.script.api.ClientInfo)1 Mappable (com.dexels.navajo.script.api.Mappable)1 MappableException (com.dexels.navajo.script.api.MappableException)1 MappingException (com.dexels.navajo.script.api.MappingException)1 SystemException (com.dexels.navajo.script.api.SystemException)1 UserException (com.dexels.navajo.script.api.UserException)1 NavajoConfigInterface (com.dexels.navajo.server.NavajoConfigInterface)1 DeflaterOutputStream (com.jcraft.jzlib.DeflaterOutputStream)1 InflaterInputStream (com.jcraft.jzlib.InflaterInputStream)1 BufferedReader (java.io.BufferedReader)1