Search in sources :

Example 6 with FatalException

use of com.dexels.navajo.script.api.FatalException in project navajo by Dexels.

the class Dispatcher method errorHandler.

/**
 * Handles errors.
 *
 * @param access
 * @param e
 * @param inMessage
 * @return the response error Navajo document.
 *
 * @throws FatalException
 */
private final Navajo errorHandler(Access access, Throwable e, Navajo inMessage) throws FatalException {
    String message = "";
    if (access != null) {
        try {
            message = e.getClass().toString() + ": " + e.getMessage() + ", " + e.toString() + ", " + e.getLocalizedMessage();
            if (message.equalsIgnoreCase("")) {
                message = "Undefined Error";
            }
            StringWriter swriter = new StringWriter();
            PrintWriter writer = new PrintWriter(swriter);
            e.printStackTrace(writer);
            message += swriter.getBuffer().toString();
            message += "\n";
            swriter = new StringWriter();
            writer = new PrintWriter(swriter);
            // Remove some messages that might contain sensitive info.
            if (inMessage != null) {
                inMessage.removeInternalMessages();
            }
            inMessage.write(writer);
            message += swriter.getBuffer().toString();
        } catch (NavajoException tbe) {
            throw new FatalException(tbe.getMessage());
        }
    } else {
        // if no access was created, use the throwable message to be able to
        // say *anything*
        message += e.getMessage();
    }
    try {
        Navajo out = generateErrorMessage(access, message, SystemException.SYSTEM_ERROR, 1, e);
        if (access != null) {
            access.setOutputDoc(out);
        }
        return out;
    } catch (Exception ne) {
        throw new FatalException(ne.getMessage(), ne);
    }
}
Also used : StringWriter(java.io.StringWriter) FatalException(com.dexels.navajo.script.api.FatalException) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) 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) PrintWriter(java.io.PrintWriter)

Example 7 with FatalException

use of com.dexels.navajo.script.api.FatalException in project navajo by Dexels.

the class LocalNavajoContext method callService.

@Override
public void callService(String service, String tenant, String username, String password, Navajo input) throws ClientException {
    if (input == null) {
        input = NavajoFactory.getInstance().createNavajo();
        input.addHeader(NavajoFactory.getInstance().createHeader(input, service, username, password, -1));
    } else {
        if (input.getHeader() == null) {
            input.addHeader(NavajoFactory.getInstance().createHeader(input, service, username, password, -1));
        } else {
            input.getHeader().setRPCName(service);
            input.getHeader().setRPCUser(getUsername());
            input.getHeader().setRPCPassword(getPassword());
        }
    }
    try {
        LocalClient lc = tenant == null ? localClients.get(DEFAULT_TENANT) : localClients.get(tenant);
        Navajo result = lc.call(input);
        result.getHeader().setRPCName(service);
        putNavajo(service, result);
    } catch (FatalException e) {
        throw (new ClientException(0, -1, "Error calling local client", e));
    }
}
Also used : FatalException(com.dexels.navajo.script.api.FatalException) LocalClient(com.dexels.navajo.script.api.LocalClient) Navajo(com.dexels.navajo.document.Navajo) ClientException(com.dexels.navajo.client.ClientException)

Example 8 with FatalException

use of com.dexels.navajo.script.api.FatalException 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");
    }
}
Also used : ServletException(javax.servlet.ServletException) FatalException(com.dexels.navajo.script.api.FatalException) Navajo(com.dexels.navajo.document.Navajo) ClientInfo(com.dexels.navajo.script.api.ClientInfo)

Example 9 with FatalException

use of com.dexels.navajo.script.api.FatalException in project navajo by Dexels.

the class CallCommand method call.

@Descriptor(value = "Call a script without input. It will bypass auth.")
public void call(CommandSession session, @Descriptor(value = "The script to call") String scr) {
    String script = scr.replaceAll("/", ".");
    Navajo n = NavajoFactory.getInstance().createNavajo();
    n.addHeader(NavajoFactory.getInstance().createHeader(n, script, "_internal_", "", -1));
    try {
        Navajo out = localClient.call(n);
        out.write(session.getConsole());
    } catch (FatalException e) {
        e.printStackTrace(session.getConsole());
    }
}
Also used : FatalException(com.dexels.navajo.script.api.FatalException) Navajo(com.dexels.navajo.document.Navajo) Descriptor(org.apache.felix.service.command.Descriptor)

Example 10 with FatalException

use of com.dexels.navajo.script.api.FatalException in project navajo by Dexels.

the class RestTmlServlet method callDirect.

private final void callDirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String service = request.getParameter("service");
    // String type = request.getParameter("type");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    logger.info(">in callDirect(): service = " + service + ", username = " + username + " class: " + getClass().getName());
    if (service == null) {
        // logger.info("Empty service specified, request originating from "
        // + request.getRemoteHost());
        logger.info("thread = " + Thread.currentThread().hashCode());
        logger.info("path = " + request.getPathInfo());
        logger.info("query = " + request.getQueryString());
        logger.info("protocol = " + request.getProtocol());
        logger.info("agent = " + request.getRemoteUser());
        logger.info("uri = " + request.getRequestURI());
        logger.info("method = " + request.getMethod());
        logger.info("contenttype = " + request.getContentType());
        logger.info("scheme = " + request.getScheme());
        logger.info("server = " + request.getServerName());
        logger.info("port = " + request.getServerPort());
        logger.info("contentlength = " + request.getContentLength());
        Enumeration<String> enm = request.getHeaderNames();
        while (enm.hasMoreElements()) {
            String key = enm.nextElement();
            String header = request.getHeader(key);
            logger.info(">>" + key + "=" + header);
        }
        return;
    }
    Navajo tbMessage;
    try {
        tbMessage = constructFromRequest(request);
        Header header = constructHeader(tbMessage, service, username, password, -1);
        tbMessage.addHeader(header);
        LocalClient lc = (LocalClient) getServletContext().getAttribute("localClient");
        if (lc == null) {
            response.sendError(500, "No navajocontext configured (in NavajoFilterServlet)");
            return;
        }
        Navajo resultMessage = lc.call(tbMessage);
        response.setContentType("text/xml");
        ServletOutputStream outputStream = response.getOutputStream();
        java.io.OutputStreamWriter out = new java.io.OutputStreamWriter(outputStream, "UTF-8");
        response.setContentType("text/xml; charset=UTF-8");
        writeOutput(resultMessage, out, service);
        // resultMessage.write(out);
        out.flush();
        out.close();
    } catch (NavajoException e) {
        logger.error("Error: ", e);
    } catch (FatalException e) {
        logger.error("Error: ", e);
    }
}
Also used : Header(com.dexels.navajo.document.Header) ServletOutputStream(javax.servlet.ServletOutputStream) FatalException(com.dexels.navajo.script.api.FatalException) LocalClient(com.dexels.navajo.script.api.LocalClient) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo)

Aggregations

FatalException (com.dexels.navajo.script.api.FatalException)12 Navajo (com.dexels.navajo.document.Navajo)11 NavajoException (com.dexels.navajo.document.NavajoException)6 Header (com.dexels.navajo.document.Header)5 AuthorizationException (com.dexels.navajo.script.api.AuthorizationException)5 UserException (com.dexels.navajo.script.api.UserException)5 IOException (java.io.IOException)5 SystemException (com.dexels.navajo.script.api.SystemException)4 TriggerException (com.dexels.navajo.server.enterprise.scheduler.TriggerException)4 ServletException (javax.servlet.ServletException)4 ClientInfo (com.dexels.navajo.script.api.ClientInfo)3 Message (com.dexels.navajo.document.Message)2 Property (com.dexels.navajo.document.Property)2 LocalClient (com.dexels.navajo.script.api.LocalClient)2 PrintWriter (java.io.PrintWriter)2 APIException (com.dexels.navajo.article.APIException)1 AuthenticationMethod (com.dexels.navajo.authentication.api.AuthenticationMethod)1 ClientException (com.dexels.navajo.client.ClientException)1 NavajoRequestEvent (com.dexels.navajo.events.types.NavajoRequestEvent)1 Access (com.dexels.navajo.script.api.Access)1