Search in sources :

Example 26 with Header

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

the class BaseRequestImpl method parseInputNavajo.

protected final Navajo parseInputNavajo(InputStream is) throws IOException {
    BufferedReader r;
    Navajo in = null;
    logger.debug("Parsing using encoding: {}", contentEncoding);
    if (contentEncoding != null && contentEncoding.equals(AsyncRequest.COMPRESS_JZLIB)) {
        r = new BufferedReader(new java.io.InputStreamReader(new InflaterInputStream(is), StandardCharsets.UTF_8));
    } else if (contentEncoding != null && contentEncoding.equals(AsyncRequest.COMPRESS_GZIP)) {
        r = new BufferedReader(new java.io.InputStreamReader(new java.util.zip.GZIPInputStream(is), StandardCharsets.UTF_8));
    } else {
        r = new BufferedReader(new java.io.InputStreamReader(is, StandardCharsets.UTF_8));
    }
    in = NavajoFactory.getInstance().createNavajo(r);
    if (in == null) {
        throw new IOException("Invalid request.");
    }
    Header header = in.getHeader();
    if (header == null) {
        throw new IOException("Empty Navajo header.");
    }
    is.close();
    return in;
}
Also used : Header(com.dexels.navajo.document.Header) InflaterInputStream(java.util.zip.InflaterInputStream) BufferedReader(java.io.BufferedReader) Navajo(com.dexels.navajo.document.Navajo) IOException(java.io.IOException)

Example 27 with Header

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

the class ServiceCommand method execute.

@Override
public JsonNode execute(ArticleRuntime runtime, ArticleContext context, Map<String, String> parameters, XMLElement element) throws APIException {
    Long startedAt = System.currentTimeMillis();
    String name = parameters.get("name");
    if (name == null) {
        throw new APIException("Command: " + this.getName() + " can't be executed without required parameters: " + name, null, APIErrorCode.InternalError);
    }
    String refresh = parameters.get("refresh");
    if ("false".equals(refresh)) {
        Navajo res = runtime.getNavajo(name);
        if (res != null) {
            runtime.pushNavajo(name, res);
            return null;
        }
    }
    String input = parameters.get("input");
    Navajo n = null;
    if (input != null) {
        n = runtime.getNavajo(input);
        if (n == null) {
            throw new APIException("Command: " + this.getName() + " supplies an 'input' parameter: " + input + ", but that navajo object can not be found" + name, null, APIErrorCode.InternalError);
        }
    }
    if (runtime.getNavajo() != null) {
        n = runtime.getNavajo();
    } else {
        n = NavajoFactory.getInstance().createNavajo();
    }
    appendTokenAttributes(runtime, n);
    final String username = runtime.getUsername();
    Header h = NavajoFactory.getInstance().createHeader(n, name, username, "", -1);
    if (runtime.getAccess() != null) {
        h.setHeaderAttribute("parentaccessid", runtime.getAccess().accessID);
    }
    n.addHeader(h);
    h.setHeaderAttribute("application", "article");
    final Navajo result = performCall(runtime, name, n, runtime.getInstance());
    statLogger.info("Finished {} ({}) in {}ms", h.getHeaderAttribute("parentaccessid"), name, (System.currentTimeMillis() - startedAt));
    runtime.pushNavajo(name, result);
    return null;
}
Also used : APIException(com.dexels.navajo.article.APIException) Header(com.dexels.navajo.document.Header) Navajo(com.dexels.navajo.document.Navajo)

Example 28 with Header

use of com.dexels.navajo.document.Header 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)

Example 29 with Header

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

the class TmlHttpServlet method constructFromRequest.

public static final Navajo constructFromRequest(HttpServletRequest request) throws NavajoException {
    Navajo result = NavajoFactory.getInstance().createNavajo();
    Enumeration<String> all = request.getParameterNames();
    // Construct TML document from request parameters.
    while (all.hasMoreElements()) {
        String parameter = all.nextElement().toString();
        if (parameter.indexOf("/") != -1) {
            StringTokenizer typedParameter = new StringTokenizer(parameter, "|");
            String propertyName = typedParameter.nextToken();
            String type = (typedParameter.hasMoreTokens() ? typedParameter.nextToken() : Property.STRING_PROPERTY);
            String value = request.getParameter(parameter);
            Message msg = com.dexels.navajo.mapping.MappingUtils.getMessageObject(parameter, null, false, result, false, "", -1);
            String propName = com.dexels.navajo.mapping.MappingUtils.getStrippedPropertyName(propertyName);
            Property prop = null;
            if (propName.indexOf(":") == -1) {
                prop = NavajoFactory.getInstance().createProperty(result, propName, type, value, 0, "", Property.DIR_IN);
                msg.addProperty(prop);
            } else {
                StringTokenizer selProp = new StringTokenizer(propName, ":");
                propertyName = selProp.nextToken();
                selProp.nextToken();
                prop = msg.getProperty(propertyName);
                if (prop == null) {
                    prop = NavajoFactory.getInstance().createProperty(result, propertyName, "+", "", Property.DIR_IN);
                    msg.addProperty(prop);
                } else {
                    prop.setType(Property.SELECTION_PROPERTY);
                    prop.setCardinality("+");
                }
                StringTokenizer allValues = new StringTokenizer(value, ",");
                while (allValues.hasMoreTokens()) {
                    String val = allValues.nextToken();
                    Selection sel = NavajoFactory.getInstance().createSelection(result, val, val, true);
                    prop.addSelection(sel);
                }
            }
        }
    }
    String service = request.getParameter("service");
    String type = request.getParameter("type");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if (username == null) {
        username = "empty";
        password = "";
    }
    if ((type == null) || (type.equals(""))) {
        type = "xml";
    }
    if (password == null) {
        password = "";
    }
    long expirationInterval = -1;
    String expiration = request.getParameter("expiration");
    if ((expiration == null) || (expiration.equals(""))) {
        expirationInterval = -1;
    } else {
        try {
            expirationInterval = Long.parseLong(expiration);
        } catch (Exception e) {
        // System.out.println("invalid expiration interval: " +
        // expiration);
        }
    }
    Header h = NavajoFactory.getInstance().createHeader(result, service, username, password, expirationInterval);
    result.addHeader(h);
    return result;
}
Also used : StringTokenizer(java.util.StringTokenizer) Message(com.dexels.navajo.document.Message) Header(com.dexels.navajo.document.Header) Selection(com.dexels.navajo.document.Selection) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) FatalException(com.dexels.navajo.script.api.FatalException) ServletException(javax.servlet.ServletException) NavajoException(com.dexels.navajo.document.NavajoException) IOException(java.io.IOException)

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