Search in sources :

Example 21 with SenderException

use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.

the class NetStorageSender method extractResult.

@Override
public String extractResult(HttpResponseHandler responseHandler, ParameterResolutionContext prc) throws SenderException, IOException {
    int statusCode = responseHandler.getStatusLine().getStatusCode();
    boolean ok = false;
    if (StringUtils.isNotEmpty(getResultStatusCodeSessionKey())) {
        prc.getSession().put(getResultStatusCodeSessionKey(), Integer.toString(statusCode));
        ok = true;
    } else {
        if (statusCode == HttpServletResponse.SC_OK) {
            ok = true;
        } else {
            if (isIgnoreRedirects()) {
                if (statusCode == HttpServletResponse.SC_MOVED_PERMANENTLY || statusCode == HttpServletResponse.SC_MOVED_TEMPORARILY || statusCode == HttpServletResponse.SC_TEMPORARY_REDIRECT) {
                    ok = true;
                }
            }
        }
    }
    if (!ok) {
        throw new SenderException(getLogPrefix() + "httpstatus " + statusCode + ": " + responseHandler.getStatusLine().getReasonPhrase() + " body: " + getResponseBodyAsString(responseHandler));
    }
    XmlBuilder result = new XmlBuilder("result");
    HttpServletResponse response = (HttpServletResponse) prc.getSession().get(IPipeLineSession.HTTP_RESPONSE_KEY);
    if (response == null) {
        XmlBuilder statuscode = new XmlBuilder("statuscode");
        statuscode.setValue(statusCode + "");
        result.addSubElement(statuscode);
        String responseString = getResponseBodyAsString(responseHandler);
        responseString = XmlUtils.skipDocTypeDeclaration(responseString.trim());
        responseString = XmlUtils.skipXmlDeclaration(responseString);
        if (statusCode == HttpURLConnection.HTTP_OK) {
            XmlBuilder message = new XmlBuilder("message");
            message.setValue(responseString, false);
            result.addSubElement(message);
        } else {
            // Validate Server-Time drift
            String dateString = responseHandler.getHeader("Date");
            if (!StringUtils.isEmpty(dateString)) {
                Date currentDate = new Date();
                DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
                long responseDate = 0;
                try {
                    Date date = format.parse(dateString);
                    responseDate = date.getTime();
                } catch (Exception e) {
                }
                if (responseDate != 0 && currentDate.getTime() - responseDate > 30 * 1000)
                    throw new SenderException("Local server Date is more than 30s out of sync with Remote server");
            }
            XmlBuilder message = new XmlBuilder("error");
            message.setValue(responseString);
            result.addSubElement(message);
            log.warn(String.format("Unexpected Response from Server: %d %s\n%s", statusCode, responseString, responseHandler.getHeaderFields()));
        }
    }
    return result.toXML();
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) HttpServletResponse(javax.servlet.http.HttpServletResponse) SenderException(nl.nn.adapterframework.core.SenderException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 22 with SenderException

use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.

the class NetStorageSender method buildUri.

/**
 * Builds the URI with the rootDirectory, optional CpCode and makes sure the
 * path never ends with a slash '/'.
 * @param path to append to the root
 * @return full path to use as endpoint
 * @throws SenderException
 */
private URIBuilder buildUri(String path) throws SenderException {
    if (!path.startsWith("/"))
        path = "/" + path;
    try {
        String url = getUrl() + getCpCode();
        if (getRootDir() != null)
            url += getRootDir();
        url += path;
        if (// The path should never end with a '/'
        url.endsWith("/"))
            url = url.substring(0, url.length() - 1);
        return new URIBuilder(url);
    } catch (URISyntaxException e) {
        throw new SenderException(e);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) SenderException(nl.nn.adapterframework.core.SenderException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 23 with SenderException

use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.

the class CmisHttpSender method getMethod.

@Override
public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList pvl, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
    HttpRequestBase method = null;
    try {
        if (getMethodType().equals("GET")) {
            method = new HttpGet(uri.build());
        } else if (getMethodType().equals("POST")) {
            HttpPost httpPost = new HttpPost(uri.build());
            // send data
            if (pvl.getParameterValue("writer") != null) {
                Output writer = (Output) pvl.getParameterValue("writer").getValue();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                Object clientCompression = pvl.getParameterValue(SessionParameter.CLIENT_COMPRESSION);
                if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
                    httpPost.setHeader("Content-Encoding", "gzip");
                    writer.write(new GZIPOutputStream(out, 4096));
                } else {
                    writer.write(out);
                }
                HttpEntity entity = new BufferedHttpEntity(new ByteArrayEntity(out.toByteArray()));
                httpPost.setEntity(entity);
                out.close();
                method = httpPost;
            }
        } else if (getMethodType().equals("PUT")) {
            HttpPut httpPut = new HttpPut(uri.build());
            // send data
            if (pvl.getParameterValue("writer") != null) {
                Output writer = (Output) pvl.getParameterValue("writer").getValue();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                Object clientCompression = pvl.getParameterValue(SessionParameter.CLIENT_COMPRESSION);
                if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
                    httpPut.setHeader("Content-Encoding", "gzip");
                    writer.write(new GZIPOutputStream(out, 4096));
                } else {
                    writer.write(out);
                }
                HttpEntity entity = new BufferedHttpEntity(new ByteArrayEntity(out.toByteArray()));
                httpPut.setEntity(entity);
                out.close();
                method = httpPut;
            }
        } else if (getMethodType().equals("DELETE")) {
            method = new HttpDelete(uri.build());
        } else {
            throw new MethodNotSupportedException("method [" + getMethodType() + "] not implemented");
        }
    } catch (Exception e) {
        throw new SenderException(e);
    }
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        log.debug("append header [" + entry.getKey() + "] with value [" + entry.getValue() + "]");
        method.addHeader(entry.getKey(), entry.getValue());
    }
    // Cmis creates it's own contentType depending on the method and bindingType
    method.setHeader("Content-Type", getContentType());
    log.debug(getLogPrefix() + "HttpSender constructed " + getMethodType() + "-method [" + method.getURI() + "] query [" + method.getURI().getQuery() + "] ");
    return method;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HttpPut(org.apache.http.client.methods.HttpPut) CmisConnectionException(org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) IOException(java.io.IOException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) SenderException(nl.nn.adapterframework.core.SenderException) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) Output(org.apache.chemistry.opencmis.client.bindings.spi.http.Output) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) SenderException(nl.nn.adapterframework.core.SenderException) Map(java.util.Map)

Example 24 with SenderException

use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.

the class CmisSender method sendMessageForActionGet.

private String sendMessageForActionGet(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    if (StringUtils.isEmpty(message)) {
        throw new SenderException(getLogPrefix() + "input string cannot be empty but must contain a documentId");
    }
    CmisObject object = null;
    try {
        object = session.getObject(session.createObjectId(message));
    } catch (CmisObjectNotFoundException e) {
        if (StringUtils.isNotEmpty(getResultOnNotFound())) {
            log.info(getLogPrefix() + "document with id [" + message + "] not found", e);
            return getResultOnNotFound();
        } else {
            throw new SenderException(e);
        }
    }
    Document document = (Document) object;
    ContentStream contentStream = document.getContentStream();
    try {
        InputStream inputStream = contentStream.getStream();
        if (isStreamResultToServlet()) {
            HttpServletResponse response = (HttpServletResponse) prc.getSession().get(IPipeLineSession.HTTP_RESPONSE_KEY);
            String contentType = contentStream.getMimeType();
            if (StringUtils.isNotEmpty(contentType)) {
                log.debug(getLogPrefix() + "setting response Content-Type header [" + contentType + "]");
                response.setHeader("Content-Type", contentType);
            }
            String contentDisposition = "attachment; filename=\"" + contentStream.getFileName() + "\"";
            log.debug(getLogPrefix() + "setting response Content-Disposition header [" + contentDisposition + "]");
            response.setHeader("Content-Disposition", contentDisposition);
            OutputStream outputStream;
            outputStream = response.getOutputStream();
            Misc.streamToStream(inputStream, outputStream);
            log.debug(getLogPrefix() + "copied document content input stream [" + inputStream + "] to output stream [" + outputStream + "]");
            return "";
        } else if (isGetProperties()) {
            if (StringUtils.isNotEmpty(fileInputStreamSessionKey)) {
                prc.getSession().put(getFileInputStreamSessionKey(), inputStream);
            } else {
                byte[] bytes = Misc.streamToBytes(inputStream);
                prc.getSession().put(getFileContentSessionKey(), Base64.encodeBase64String(bytes));
            }
            XmlBuilder cmisXml = new XmlBuilder("cmis");
            XmlBuilder propertiesXml = new XmlBuilder("properties");
            for (Iterator it = document.getProperties().iterator(); it.hasNext(); ) {
                Property property = (Property) it.next();
                propertiesXml.addSubElement(getPropertyXml(property));
            }
            cmisXml.addSubElement(propertiesXml);
            return cmisXml.toXML();
        } else {
            return Misc.streamToString(inputStream, null, false);
        }
    } catch (IOException e) {
        throw new SenderException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Document(org.apache.chemistry.opencmis.client.api.Document) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) Iterator(java.util.Iterator) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) SenderException(nl.nn.adapterframework.core.SenderException) Property(org.apache.chemistry.opencmis.client.api.Property)

Example 25 with SenderException

use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.

the class CmisSender method getSession.

public Session getSession(ParameterResolutionContext prc) throws SenderException {
    if (session == null || !isKeepSession()) {
        String authAlias_work = null;
        String userName_work = null;
        String password_work = null;
        ParameterValueList pvl = null;
        try {
            if (prc != null && paramList != null) {
                pvl = prc.getValues(paramList);
                if (pvl != null) {
                    ParameterValue pv = pvl.getParameterValue("authAlias");
                    if (pv != null) {
                        authAlias_work = (String) pv.getValue();
                    }
                    pv = pvl.getParameterValue("userName");
                    if (pv != null) {
                        userName_work = (String) pv.getValue();
                    }
                    pv = pvl.getParameterValue("password");
                    if (pv != null) {
                        password_work = (String) pv.getValue();
                    }
                }
            }
        } catch (ParameterException e) {
            throw new SenderException(getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e);
        }
        if (authAlias_work == null) {
            authAlias_work = getAuthAlias();
        }
        if (userName_work == null) {
            userName_work = getUserName();
        }
        if (password_work == null) {
            password_work = getPassword();
        }
        CredentialFactory cf = new CredentialFactory(authAlias_work, userName_work, password_work);
        session = connect(cf.getUsername(), cf.getPassword());
    }
    return session;
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException)

Aggregations

SenderException (nl.nn.adapterframework.core.SenderException)130 TimeOutException (nl.nn.adapterframework.core.TimeOutException)41 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)37 IOException (java.io.IOException)36 SQLException (java.sql.SQLException)25 HashMap (java.util.HashMap)21 ParameterException (nl.nn.adapterframework.core.ParameterException)21 PreparedStatement (java.sql.PreparedStatement)20 Map (java.util.Map)20 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)18 Iterator (java.util.Iterator)17 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)16 InputStream (java.io.InputStream)15 ResultSet (java.sql.ResultSet)13 Element (org.w3c.dom.Element)13 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)12 Date (java.util.Date)10 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)10 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)10 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9