Search in sources :

Example 1 with EndOfStreamException

use of com.predic8.membrane.core.util.EndOfStreamException in project service-proxy by membrane.

the class Request method parseStartLine.

@Override
public void parseStartLine(InputStream in) throws IOException, EndOfStreamException {
    try {
        String firstLine = HttpUtil.readLine(in);
        Matcher matcher = pattern.matcher(firstLine);
        if (matcher.find()) {
            method = matcher.group(1);
            uri = matcher.group(2);
            version = matcher.group(3);
        } else if (stompPattern.matcher(firstLine).find()) {
            method = firstLine;
            uri = "";
            version = "STOMP";
        } else {
            throw new EOFWhileReadingFirstLineException(firstLine);
        }
    } catch (EOFWhileReadingLineException e) {
        if (e.getLineSoFar().length() == 0)
            // happens regularly at the end of a keep-alive connection
            throw new NoMoreRequestsException();
        throw new EOFWhileReadingFirstLineException(e.getLineSoFar());
    }
}
Also used : Matcher(java.util.regex.Matcher) NoMoreRequestsException(com.predic8.membrane.core.transport.http.NoMoreRequestsException) EOFWhileReadingFirstLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException) EOFWhileReadingLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingLineException)

Example 2 with EndOfStreamException

use of com.predic8.membrane.core.util.EndOfStreamException in project service-proxy by membrane.

the class Response method parseStartLine.

@Override
public void parseStartLine(InputStream in) throws IOException, EndOfStreamException {
    String line;
    try {
        line = HttpUtil.readLine(in);
    } catch (EOFWhileReadingLineException e) {
        if (e.getLineSoFar().length() == 0)
            throw new NoResponseException(e);
        throw new EOFWhileReadingFirstLineException(e.getLineSoFar());
    }
    Matcher matcher = pattern.matcher(line);
    boolean find = matcher.find();
    if (!find) {
        throw new RuntimeException("Invalid server response: " + line);
    }
    version = matcher.group(1);
    statusCode = Integer.parseInt(matcher.group(2));
    statusMessage = matcher.group(4);
}
Also used : Matcher(java.util.regex.Matcher) EOFWhileReadingFirstLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException) NoResponseException(com.predic8.membrane.core.transport.http.NoResponseException) EOFWhileReadingLineException(com.predic8.membrane.core.transport.http.EOFWhileReadingLineException)

Example 3 with EndOfStreamException

use of com.predic8.membrane.core.util.EndOfStreamException in project service-proxy by membrane.

the class WebSocketStompReassembler method convertToExchange.

private Exchange convertToExchange(WebSocketFrame wsStompFrame) throws IOException, EndOfStreamException {
    byte[] realPayload = new byte[(int) wsStompFrame.getPayloadLength()];
    System.arraycopy(wsStompFrame.getPayload(), 0, realPayload, 0, (int) wsStompFrame.getPayloadLength());
    if (wsStompFrame.getPayloadLength() == 0)
        throw new IOException("Empty STOMP frame.");
    ByteArrayInputStream bais = new ByteArrayInputStream(wsStompFrame.getPayload(), 0, (int) wsStompFrame.getPayloadLength() - 1);
    Request request = new Request();
    if (isHeartBeat(wsStompFrame)) {
        request.setMethod("");
        request.setBody(new Body(bais));
    } else {
        if (wsStompFrame.getPayload()[(int) wsStompFrame.getPayloadLength() - 1] != 0)
            throw new IOException("STOMP frame is not terminated by \\0.");
        request.read(bais, true);
    }
    Exchange result = new Exchange(null);
    result.setRequest(request);
    if (wsStompFrame.getOriginalExchange() != null)
        result.setProperty(Exchange.WS_ORIGINAL_EXCHANGE, wsStompFrame.getOriginalExchange());
    return result;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) Request(com.predic8.membrane.core.http.Request) IOException(java.io.IOException) Body(com.predic8.membrane.core.http.Body)

Example 4 with EndOfStreamException

use of com.predic8.membrane.core.util.EndOfStreamException in project service-proxy by membrane.

the class XMLContentFilter method removeMatchingElements.

/**
 * Removes parts of an XML document based on an XPath expression.
 *
 * If the message is not valid XML, it is left unchanged.
 */
public void removeMatchingElements(Message message) {
    try {
        Message xop = null;
        try {
            xop = xopReconstitutor.getReconstitutedMessage(message);
        } catch (ParseException e) {
        } catch (EndOfStreamException e) {
        } catch (FactoryConfigurationError e) {
        }
        if (elementFinder != null && !elementFinder.matches(xop != null ? xop.getBodyAsStream() : message.getBodyAsStream())) {
            return;
        }
        DocumentBuilder db = createDocumentBuilder();
        Document d;
        try {
            d = db.parse(xop != null ? xop.getBodyAsStream() : message.getBodyAsStream());
        } finally {
            db.reset();
        }
        removeElementsIfNecessary(message, xop, d);
    } catch (SAXException e) {
        return;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (XMLStreamException e) {
        return;
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new RuntimeException(e);
    }
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) Message(com.predic8.membrane.core.http.Message) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XMLStreamException(javax.xml.stream.XMLStreamException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParseException(javax.mail.internet.ParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) FactoryConfigurationError(javax.xml.stream.FactoryConfigurationError) TransformerException(javax.xml.transform.TransformerException)

Example 5 with EndOfStreamException

use of com.predic8.membrane.core.util.EndOfStreamException in project service-proxy by membrane.

the class XOPReconstitutor method getReconstitutedMessage.

/**
 * @return reassembled SOAP message or null if message is not SOAP or not multipart
 */
public Message getReconstitutedMessage(Message message) throws ParseException, MalformedStreamException, IOException, EndOfStreamException, XMLStreamException, FactoryConfigurationError {
    ContentType contentType = message.getHeader().getContentTypeObject();
    if (contentType == null || contentType.getPrimaryType() == null)
        return null;
    if (!contentType.getPrimaryType().equals("multipart") || !contentType.getSubType().equals("related"))
        return null;
    String type = contentType.getParameter("type");
    if (!"application/xop+xml".equals(type))
        return null;
    String start = contentType.getParameter("start");
    if (start == null)
        return null;
    String boundary = contentType.getParameter("boundary");
    if (boundary == null)
        return null;
    HashMap<String, Part> parts = split(message, boundary);
    Part startPart = parts.get(start);
    if (startPart == null)
        return null;
    ContentType innerContentType = new ContentType(startPart.getHeader().getContentType());
    if (!innerContentType.getPrimaryType().equals("application") || !innerContentType.getSubType().equals("xop+xml"))
        return null;
    byte[] body = fillInXOPParts(startPart.getInputStream(), parts);
    Message m = new Message() {

        @Override
        protected void parseStartLine(InputStream in) throws IOException, EndOfStreamException {
            throw new RuntimeException("not implemented.");
        }

        @Override
        public String getStartLine() {
            throw new RuntimeException("not implemented.");
        }

        @Override
        public <T extends Message> T createSnapshot() {
            throw new RuntimeException("not implemented.");
        }
    };
    m.setBodyContent(body);
    String reconstitutedContentType = innerContentType.getParameter("type");
    if (reconstitutedContentType != null)
        m.getHeader().add(Header.CONTENT_TYPE, reconstitutedContentType);
    return m;
}
Also used : ContentType(javax.mail.internet.ContentType) Message(com.predic8.membrane.core.http.Message) InputStream(java.io.InputStream)

Aggregations

IOException (java.io.IOException)4 EOFWhileReadingFirstLineException (com.predic8.membrane.core.transport.http.EOFWhileReadingFirstLineException)3 EOFWhileReadingLineException (com.predic8.membrane.core.transport.http.EOFWhileReadingLineException)3 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)3 Exchange (com.predic8.membrane.core.exchange.Exchange)2 Message (com.predic8.membrane.core.http.Message)2 Request (com.predic8.membrane.core.http.Request)2 Matcher (java.util.regex.Matcher)2 Body (com.predic8.membrane.core.http.Body)1 Header (com.predic8.membrane.core.http.Header)1 Response (com.predic8.membrane.core.http.Response)1 AbortException (com.predic8.membrane.core.transport.http.AbortException)1 LineTooLongException (com.predic8.membrane.core.transport.http.LineTooLongException)1 NoMoreRequestsException (com.predic8.membrane.core.transport.http.NoMoreRequestsException)1 NoResponseException (com.predic8.membrane.core.transport.http.NoResponseException)1 DNSCache (com.predic8.membrane.core.util.DNSCache)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 SocketException (java.net.SocketException)1