Search in sources :

Example 1 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class CMSenderOneMessageImpl method doHttpPost.

private void doHttpPost(final String urlString, final String requestString) {
    final HttpClient client = HttpClientBuilder.create().build();
    final HttpPost post = new HttpPost(urlString);
    post.setEntity(new StringEntity(requestString, Charset.forName("UTF-8")));
    try {
        final HttpResponse response = client.execute(post);
        final int statusCode = response.getStatusLine().getStatusCode();
        LOG.debug("Response Code : {}", statusCode);
        if (statusCode == 400) {
            throw new CMDirectException("CM Component and CM API show some kind of inconsistency. " + "CM is complaining about not using a post method for the request. And this component only uses POST requests. What happens?");
        }
        if (statusCode != 200) {
            throw new CMDirectException("CM Component and CM API show some kind of inconsistency. The component expects the status code to be 200 or 400. New api released? ");
        }
        // So we have 200 status code...
        // The response type is 'text/plain' and contains the actual
        // result of the request processing.
        // We obtaing the result text
        final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        final StringBuffer result = new StringBuffer();
        String line = null;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        // ... and process it
        line = result.toString();
        if (!line.isEmpty()) {
            // Line is not empty = error
            LOG.debug("Result of the request processing: FAILED\n{}", line);
            if (line.contains(CMConstants.ERROR_UNKNOWN)) {
                throw new UnknownErrorException();
            } else if (line.contains(CMConstants.ERROR_NO_ACCOUNT)) {
                throw new NoAccountFoundForProductTokenException();
            } else if (line.contains(CMConstants.ERROR_INSUFICIENT_BALANCE)) {
                throw new InsufficientBalanceException();
            } else if (line.contains(CMConstants.ERROR_UNROUTABLE_MESSAGE)) {
                throw new UnroutableMessageException();
            } else if (line.contains(CMConstants.ERROR_INVALID_PRODUCT_TOKEN)) {
                throw new InvalidProductTokenException();
            } else {
                // responsibility
                throw new CMResponseException("CHECK ME. I am not expecting this. ");
            }
        }
        // Ok. Line is EMPTY - successfully submitted
        LOG.debug("Result of the request processing: Successfully submited");
    } catch (final IOException io) {
        throw new CMDirectException(io);
    } catch (Throwable t) {
        if (!(t instanceof CMDirectException)) {
            // Chain it
            t = new CMDirectException(t);
        }
        throw (CMDirectException) t;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CMDirectException(org.apache.camel.component.cm.exceptions.CMDirectException) UnknownErrorException(org.apache.camel.component.cm.exceptions.cmresponse.UnknownErrorException) InputStreamReader(java.io.InputStreamReader) CMResponseException(org.apache.camel.component.cm.exceptions.cmresponse.CMResponseException) InsufficientBalanceException(org.apache.camel.component.cm.exceptions.cmresponse.InsufficientBalanceException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) UnroutableMessageException(org.apache.camel.component.cm.exceptions.cmresponse.UnroutableMessageException) InvalidProductTokenException(org.apache.camel.component.cm.exceptions.cmresponse.InvalidProductTokenException) HttpClient(org.apache.http.client.HttpClient) BufferedReader(java.io.BufferedReader) NoAccountFoundForProductTokenException(org.apache.camel.component.cm.exceptions.cmresponse.NoAccountFoundForProductTokenException)

Example 2 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class CxfRsRelayTest method testJaxrsRelayRoute.

/**
     * That test builds a route chaining two cxfrs endpoints. It shows a request
     * sent to the first one will be correctly transferred and consumed by the
     * other one.
     */
@Test
public void testJaxrsRelayRoute() throws Exception {
    final Main main = new Main();
    try {
        main.setApplicationContextUri("org/apache/camel/component/cxf/jaxrs/CxfRsSpringRelay.xml");
        main.start();
        Thread t = new Thread(new Runnable() {

            /**
                 * Sends a request to the first endpoint in the route
                 */
            public void run() {
                try {
                    JAXRSClientFactory.create("http://localhost:" + port6 + "/CxfRsRelayTest/rest", UploadService.class).upload(CamelRouteBuilder.class.getResourceAsStream(SAMPLE_CONTENT_PATH), SAMPLE_NAME);
                } catch (Exception e) {
                    log.warn("Error uploading to http://localhost:" + port6 + "/CxfRsRelayTest/rest", e);
                }
            }
        });
        t.start();
        LATCH.await(10, TimeUnit.SECONDS);
        assertEquals(SAMPLE_NAME, name);
        StringWriter writer = new StringWriter();
        IOUtils.copyAndCloseInput(new InputStreamReader(CamelRouteBuilder.class.getResourceAsStream(SAMPLE_CONTENT_PATH)), writer);
        assertEquals(writer.toString(), content);
    } finally {
        main.stop();
    }
}
Also used : StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) Main(org.apache.camel.spring.Main) Test(org.junit.Test)

Example 3 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class OrderTest method checkWsdl.

public void checkWsdl(InputStream in) throws Exception {
    boolean containsOrderComplexType = false;
    LineNumberReader reader = new LineNumberReader(new InputStreamReader(in));
    String line;
    while ((line = reader.readLine()) != null) {
        if (line.contains("complexType name=\"order\"")) {
            containsOrderComplexType = true;
        // break;
        }
    }
    if (!containsOrderComplexType) {
        throw new RuntimeException("WSDL does not contain complex type defintion for class Order");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LineNumberReader(java.io.LineNumberReader)

Example 4 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class FlatpackEndpoint method createDelimitedParser.

public Parser createDelimitedParser(Exchange exchange) throws InvalidPayloadException, IOException {
    Reader bodyReader = exchange.getIn().getMandatoryBody(Reader.class);
    Parser parser;
    if (ObjectHelper.isEmpty(getResourceUri())) {
        parser = getParserFactory().newDelimitedParser(bodyReader, delimiter, textQualifier);
    } else {
        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), resourceUri);
        InputStreamReader reader = new InputStreamReader(is, IOHelper.getCharsetName(exchange));
        parser = getParserFactory().newDelimitedParser(reader, bodyReader, delimiter, textQualifier, ignoreFirstRecord);
    }
    if (isAllowShortLines()) {
        parser.setHandlingShortLines(true);
        parser.setIgnoreParseWarnings(true);
    }
    if (isIgnoreExtraColumns()) {
        parser.setIgnoreExtraColumns(true);
        parser.setIgnoreParseWarnings(true);
    }
    return parser;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Parser(net.sf.flatpack.Parser)

Example 5 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class FlatpackEndpoint method createFixedParser.

protected Parser createFixedParser(String resourceUri, Reader bodyReader) throws IOException {
    InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(), resourceUri);
    InputStreamReader reader = new InputStreamReader(is);
    Parser parser = getParserFactory().newFixedLengthParser(reader, bodyReader);
    if (isAllowShortLines()) {
        parser.setHandlingShortLines(true);
        parser.setIgnoreParseWarnings(true);
    }
    if (isIgnoreExtraColumns()) {
        parser.setIgnoreExtraColumns(true);
        parser.setIgnoreParseWarnings(true);
    }
    return parser;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Parser(net.sf.flatpack.Parser)

Aggregations

InputStreamReader (java.io.InputStreamReader)4181 BufferedReader (java.io.BufferedReader)2938 IOException (java.io.IOException)1843 InputStream (java.io.InputStream)1083 FileInputStream (java.io.FileInputStream)767 ArrayList (java.util.ArrayList)494 URL (java.net.URL)471 File (java.io.File)448 Reader (java.io.Reader)446 Test (org.junit.Test)336 HttpURLConnection (java.net.HttpURLConnection)253 ByteArrayInputStream (java.io.ByteArrayInputStream)231 OutputStreamWriter (java.io.OutputStreamWriter)218 FileNotFoundException (java.io.FileNotFoundException)210 HashMap (java.util.HashMap)171 Socket (java.net.Socket)170 OutputStream (java.io.OutputStream)150 URLConnection (java.net.URLConnection)148 StringWriter (java.io.StringWriter)135 Path (org.apache.hadoop.fs.Path)123