Search in sources :

Example 1 with ConditionErrorException

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

the class CompiledScript method run.

/*
     * (non-Javadoc)
     * 
     * @see
     * com.dexels.navajo.script.api.CompiledScriptInterface#run(com.dexels.navajo
     * .api.Access)
     */
@Override
public final void run(Access access) throws Exception {
    myAccess = access;
    long start = System.currentTimeMillis();
    try {
        dumpRequest();
        setValidations();
        currentParamMsg = access.getInDoc().getMessage("__parms__");
        ConditionData[] conditions = getValidationRules(access);
        boolean conditionsFailed = false;
        if (conditions != null && conditions.length > 0) {
            Navajo outMessage = access.getOutputDoc();
            Message[] failed = checkValidationRules(conditions, access.getInDoc(), outMessage, access);
            if (failed != null) {
                conditionsFailed = true;
                access.setExitCode(Access.EXIT_VALIDATION_ERR);
                Message msg = NavajoFactory.getInstance().createMessage(outMessage, "ConditionErrors");
                outMessage.addMessage(msg);
                msg.setType(Message.MSG_TYPE_ARRAY);
                for (int i = 0; i < failed.length; i++) {
                    msg.addMessage(failed[i]);
                }
            }
        }
        if (!conditionsFailed) {
            try {
                execute(access);
                access.setExitCode(Access.EXIT_OK);
            } catch (com.dexels.navajo.mapping.BreakEvent be) {
                access.setExitCode(Access.EXIT_BREAK);
                throw be;
            } catch (UserException e) {
                access.setExitCode(Access.EXIT_USEREXCEPTION);
                throw e;
            } catch (ConditionErrorException e) {
                access.setExitCode(Access.EXIT_VALIDATION_ERR);
                throw e;
            } catch (Exception e) {
                access.setExitCode(Access.EXIT_EXCEPTION);
                throw e;
            } finally {
                finalBlock(access);
            }
        }
    } finally {
        dumpResponse();
        // Release acquired locks.
        for (Lock l : acquiredLocks) {
            try {
                l.unlock();
            } catch (Throwable t) {
            }
        }
        acquiredLocks.clear();
        access.processingTime = (int) (System.currentTimeMillis() - start);
    }
}
Also used : ConditionData(com.dexels.navajo.server.ConditionData) Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SystemException(com.dexels.navajo.script.api.SystemException) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) CompilationException(com.dexels.navajo.script.api.CompilationException) Lock(java.util.concurrent.locks.Lock) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) UserException(com.dexels.navajo.script.api.UserException)

Example 2 with ConditionErrorException

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

the class ServiceCommand method handleError.

private void handleError(Navajo result) throws UserException, AuthorizationException, ConditionErrorException {
    Message error = result.getMessage("error");
    if (error != null) {
        String errMsg = error.getProperty("message").getValue();
        String errCode = error.getProperty("code").getValue();
        int errorCode = -1;
        try {
            errorCode = Integer.parseInt(errCode);
        } catch (NumberFormatException e) {
            logger.error("Error: ", e);
        }
        throw new UserException(errorCode, errMsg);
    }
    boolean authenticationError = false;
    Message aaaError = result.getMessage(AuthorizationException.AUTHENTICATION_ERROR_MESSAGE);
    if (aaaError == null) {
        aaaError = result.getMessage(AuthorizationException.AUTHORIZATION_ERROR_MESSAGE);
    } else {
        authenticationError = true;
    }
    if (aaaError != null) {
        throw new AuthorizationException(authenticationError, !authenticationError, aaaError.getProperty("User").getValue(), aaaError.getProperty("Message").getValue());
    }
    if (result.getMessage("ConditionErrors") != null) {
        throw new ConditionErrorException(result);
    }
}
Also used : ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) Message(com.dexels.navajo.document.Message) AuthorizationException(com.dexels.navajo.script.api.AuthorizationException) UserException(com.dexels.navajo.script.api.UserException)

Example 3 with ConditionErrorException

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

the class ServiceCommand method performCall.

protected Navajo performCall(ArticleRuntime runtime, String name, Navajo n, String instance) throws APIException {
    try {
        Navajo result = dispatcher.handle(n, instance, true);
        handleError(result);
        return result;
    } catch (UserException | AuthorizationException | FatalException e) {
        throw new APIException(e.getMessage(), e, APIErrorCode.InternalError);
    } catch (ConditionErrorException e) {
        throw new APIException(e.getMessage(), e, APIErrorCode.ConditionError);
    }
}
Also used : ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) APIException(com.dexels.navajo.article.APIException) AuthorizationException(com.dexels.navajo.script.api.AuthorizationException) FatalException(com.dexels.navajo.script.api.FatalException) Navajo(com.dexels.navajo.document.Navajo) UserException(com.dexels.navajo.script.api.UserException)

Example 4 with ConditionErrorException

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

the class RESTAdapter method setDoSend.

public void setDoSend(String url, Navajo od) throws UserException, ConditionErrorException, SystemException {
    this.url = url.trim();
    if (dateFormat != null && !dateFormat.equals("")) {
        json.setDateFormat(new SimpleDateFormat(dateFormat));
    }
    Writer w = new StringWriter();
    Binary bContent = new Binary();
    if (textContent == null) {
        // Remove globals and parms message
        if (od.getMessage("__globals__") != null)
            od.removeMessage("__globals__");
        if (od.getMessage(Message.MSG_PARAMETERS_BLOCK) != null)
            od.removeMessage(Message.MSG_PARAMETERS_BLOCK);
        if (od.getMessage(Message.MSG_TOKEN_BLOCK) != null)
            od.removeMessage(Message.MSG_TOKEN_BLOCK);
        if (od.getMessage(Message.MSG_AAA_BLOCK) != null)
            od.removeMessage(Message.MSG_AAA_BLOCK);
        try {
            json.format(od, w, true);
            bContent.getOutputStream().write(w.toString().getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            logger.error("Exception on parsing input navajo as JSON! Not performing REST call!");
            throw new UserException(e.getMessage(), e);
        }
    } else {
        try {
            bContent.getOutputStream().write(textContent.toString().getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            logger.error("IOException on writing textcontent! Not performing REST call!");
            throw new UserException(e.getMessage(), e);
        }
    }
    HTTPMap http = new HTTPMap();
    setupHttpMap(http, bContent);
    http.setDoSend(true);
    Binary result = http.getResult();
    responseCode = http.getResponseCode();
    responseMessage = http.getResponseMessage();
    try {
        if (result == null) {
            throw new UserException(-1, "Null result");
        }
        if (responseCode >= 300) {
            logger.warn("Got a non-200 response code: {}!", responseCode);
            if (breakOnException) {
                throw new UserException(responseCode, responseMessage);
            }
        }
        rawResult = new String(result.getData());
        if (jsonResponse) {
            if (http.getResponseContentType() != null && http.getResponseContentType().contains("application/json")) {
                try {
                    inDoc = json.parse(result.getDataAsStream(), topMessage);
                } catch (Throwable t) {
                    logger.warn("Unable to parse response as JSON!", t);
                    if (breakOnException) {
                        throw t;
                    }
                }
            } else if (http.getResponseContentType() == null) {
                logger.info("No response content type - creating empty navajo as response");
                inDoc = NavajoFactory.getInstance().createNavajo();
            } else {
                logger.warn("Unexpected output content type: {}", http.getResponseContentType());
                if (breakOnException) {
                    throw new UserException(-1, "Unexpected content type: " + http.getResponseContentType());
                }
            }
        } else {
            logger.debug("Non-json response - creating empty Navajo");
            inDoc = NavajoFactory.getInstance().createNavajo();
        }
    } catch (Throwable e) {
        logger.error("Exception on getting response", e);
        if (breakOnException) {
            logger.warn("Raw response data: {}", rawResult);
            throw new UserException(e.getMessage(), e);
        } else {
            logger.warn("Exception on getting response, but breakOnException was set. Continuing!");
        }
    }
    if (inDoc == null) {
        logger.warn("No indoc - creating empty one");
        inDoc = NavajoFactory.getInstance().createNavajo();
    }
}
Also used : StringWriter(java.io.StringWriter) Binary(com.dexels.navajo.document.types.Binary) UserException(com.dexels.navajo.script.api.UserException) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) StringWriter(java.io.StringWriter) Writer(java.io.Writer) IOException(java.io.IOException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SystemException(com.dexels.navajo.script.api.SystemException) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException)

Example 5 with ConditionErrorException

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

the class NavajoMap method setDoSend.

/**
 * Use this method to call another Navajo webservice. If server is not specified, the Navajo server that is used to handle this request is also used to
 * handle the new request.
 *
 * @param method
 * @throws UserException
 */
public void setDoSend(String method, Navajo od) throws UserException, ConditionErrorException, SystemException {
    if (serviceCalled) {
        logger.warn("DO NOT USE A NAVAJOMAP TO CALL A SECOND WEBSERVICE, USE NEW NAVAJOMAP INSTEAD");
    }
    // Reset current msgPointer when performing new doSend.
    msgPointer = null;
    setMethod(method);
    this.outDoc = od;
    this.username = (username == null) ? this.access.rpcUser : username;
    this.password = (password == null) ? this.access.rpcPwd : password;
    this.method = method;
    if (password == null)
        password = "";
    try {
        if (this.resource != null) {
            serviceCalled = true;
            AsyncClient ac = NavajoClientResourceManager.getInstance().getAsyncClient(this.resource);
            if (ac == null) {
                throw new UserException(-1, "No external resource found for: " + this.resource);
            }
            ac.callService(outDoc, method, this);
        } else if (server != null) {
            // External request.
            try {
                ManualAsyncClient ac = AsyncClientFactory.getManualInstance();
                if (ac == null) {
                    logger.warn("unable to find async client - cannot perform navajomap call!");
                    throw new UserException(-1, "AsyncClient null");
                }
                String server = this.server.startsWith("http") ? this.server : "http://" + this.server;
                Integer timeout = null;
                if (serverTimeout > -1) {
                    timeout = serverTimeout;
                }
                ac.callService(server, username, password, outDoc, method, this, timeout);
            } catch (Exception e) {
                throw new UserException(-1, e.getMessage(), e);
            }
            serviceCalled = true;
        } else // Internal request.
        {
            inDoc = null;
            serviceFinished = false;
            if (block) {
                this.run();
            } else {
                SchedulerRegistry.submit(this, lowPriority);
            }
            serviceCalled = true;
            if (getException() != null) {
                if (getException() instanceof ConditionErrorException) {
                    throw (ConditionErrorException) getException();
                } else if (getException() instanceof UserException) {
                    throw (UserException) getException();
                } else if (getException() instanceof SystemException) {
                    throw (SystemException) getException();
                } else {
                    throw new SystemException(-1, "", getException());
                }
            }
        }
    } catch (NavajoException | IOException e) {
        throw new SystemException("Error connecting to remote server", e);
    }
}
Also used : ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) ManualAsyncClient(com.dexels.navajo.client.async.ManualAsyncClient) SystemException(com.dexels.navajo.script.api.SystemException) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) IOException(java.io.IOException) AsyncClient(com.dexels.navajo.client.async.AsyncClient) ManualAsyncClient(com.dexels.navajo.client.async.ManualAsyncClient) 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)

Aggregations

ConditionErrorException (com.dexels.navajo.server.ConditionErrorException)7 UserException (com.dexels.navajo.script.api.UserException)6 Message (com.dexels.navajo.document.Message)4 AuthorizationException (com.dexels.navajo.script.api.AuthorizationException)4 MappableException (com.dexels.navajo.script.api.MappableException)3 SystemException (com.dexels.navajo.script.api.SystemException)3 Navajo (com.dexels.navajo.document.Navajo)2 NavajoException (com.dexels.navajo.document.NavajoException)2 IOException (java.io.IOException)2 APIException (com.dexels.navajo.article.APIException)1 AsyncClient (com.dexels.navajo.client.async.AsyncClient)1 ManualAsyncClient (com.dexels.navajo.client.async.ManualAsyncClient)1 Binary (com.dexels.navajo.document.types.Binary)1 CompilationException (com.dexels.navajo.script.api.CompilationException)1 FatalException (com.dexels.navajo.script.api.FatalException)1 ConditionData (com.dexels.navajo.server.ConditionData)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 PrintWriter (java.io.PrintWriter)1