Search in sources :

Example 1 with BufferedReader

use of java.io.BufferedReader 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 BufferedReader

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

the class DefaultPropertiesResolver method loadPropertiesFromClasspath.

protected Properties loadPropertiesFromClasspath(CamelContext context, boolean ignoreMissingLocation, PropertiesLocation location) throws IOException {
    Properties answer = new Properties();
    String path = location.getPath();
    InputStream is = context.getClassResolver().loadResourceAsStream(path);
    Reader reader = null;
    if (is == null) {
        if (!ignoreMissingLocation && !location.isOptional()) {
            throw new FileNotFoundException("Properties file " + path + " not found in classpath");
        }
    } else {
        try {
            if (propertiesComponent.getEncoding() != null) {
                reader = new BufferedReader(new InputStreamReader(is, propertiesComponent.getEncoding()));
                answer.load(reader);
            } else {
                answer.load(is);
            }
        } finally {
            IOHelper.close(reader, is);
        }
    }
    return answer;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) BufferedReader(java.io.BufferedReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Properties(java.util.Properties)

Example 3 with BufferedReader

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

the class FileDataSet method readSourceFile.

// Implementation methods
//-------------------------------------------------------------------------
private void readSourceFile() throws IOException {
    List<Object> bodies = new LinkedList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) {
        Scanner scanner = new Scanner(br);
        scanner.useDelimiter(delimiter);
        while (scanner.hasNext()) {
            String nextPayload = scanner.next();
            if ((nextPayload != null) && (nextPayload.length() > 0)) {
                bodies.add(nextPayload);
            }
        }
        setDefaultBodies(bodies);
    }
}
Also used : Scanner(java.util.Scanner) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) LinkedList(java.util.LinkedList)

Example 4 with BufferedReader

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

the class AnnotationTypeConverterLoader method findPackages.

protected void findPackages(Set<String> packages, ClassLoader classLoader) throws IOException {
    Enumeration<URL> resources = classLoader.getResources(META_INF_SERVICES);
    while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        String path = url.getPath();
        if (!visitedURIs.contains(path)) {
            // remember we have visited this uri so we wont read it twice
            visitedURIs.add(path);
            LOG.debug("Loading file {} to retrieve list of packages, from url: {}", META_INF_SERVICES, url);
            BufferedReader reader = IOHelper.buffered(new InputStreamReader(url.openStream(), UTF8));
            try {
                while (true) {
                    String line = reader.readLine();
                    if (line == null) {
                        break;
                    }
                    line = line.trim();
                    if (line.startsWith("#") || line.length() == 0) {
                        continue;
                    }
                    tokenize(packages, line);
                }
            } finally {
                IOHelper.close(reader, null, LOG);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URL(java.net.URL)

Example 5 with BufferedReader

use of java.io.BufferedReader in project antlrworks by antlr.

the class StatisticsReporter method submitStats.

protected boolean submitStats(String id, String type, String data) {
    if (id == null) {
        error = "cannot submit stat with a null id";
        return false;
    }
    StringBuilder param = new StringBuilder();
    param.append(URL_SEND_STATS);
    param.append("ID=");
    param.append(id);
    param.append("&type=");
    param.append(type);
    param.append("&data=");
    param.append(XJUtils.encodeToURL(data));
    URLConnection urc;
    URL url;
    BufferedReader br;
    boolean success = false;
    try {
        url = new URL(param.toString());
        urc = url.openConnection();
        br = new BufferedReader(new InputStreamReader(urc.getInputStream()));
        success = br.readLine().equalsIgnoreCase("OK");
        br.close();
    } catch (IOException e) {
        error = e.toString();
    }
    return success;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URLConnection(java.net.URLConnection) URL(java.net.URL)

Aggregations

BufferedReader (java.io.BufferedReader)4790 InputStreamReader (java.io.InputStreamReader)2944 IOException (java.io.IOException)2260 FileReader (java.io.FileReader)1112 File (java.io.File)818 InputStream (java.io.InputStream)730 ArrayList (java.util.ArrayList)676 FileInputStream (java.io.FileInputStream)626 URL (java.net.URL)390 Test (org.junit.Test)367 FileNotFoundException (java.io.FileNotFoundException)329 StringReader (java.io.StringReader)282 BufferedWriter (java.io.BufferedWriter)205 HashMap (java.util.HashMap)199 Matcher (java.util.regex.Matcher)196 OutputStreamWriter (java.io.OutputStreamWriter)190 HttpURLConnection (java.net.HttpURLConnection)190 PrintWriter (java.io.PrintWriter)187 Reader (java.io.Reader)169 Socket (java.net.Socket)151