Search in sources :

Example 1 with SwaggerError

use of com.crivano.swaggerservlet.SwaggerError in project balcaovirtual by trf2-jus-br.

the class CertidaoEmitirRequisitanteCpfcnpjPost method fetch.

public static FetchResponse fetch(String url, String authorization, String method, String body) throws Exception {
    HttpURLConnection con = null;
    URL obj = new URL(url);
    con = (HttpURLConnection) obj.openConnection();
    con.setRequestProperty("User-Agent", "SwaggerServlets");
    if (authorization != null)
        con.setRequestProperty("Authorization", authorization);
    con.setRequestMethod(method);
    if ("POST".equals(method)) {
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        // set timeout to 5 seconds
        con.setConnectTimeout(5000);
        // set read timeout to 20 seconds
        con.setReadTimeout(20000);
        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(body.getBytes(StandardCharsets.UTF_8));
        wr.flush();
        wr.close();
    }
    con.setInstanceFollowRedirects(false);
    int responseCode = con.getResponseCode();
    while (responseCode == 302) {
        String newUrl = con.getHeaderField("Location");
        String cookies = con.getHeaderField("Set-Cookie");
        if (!newUrl.startsWith("http://") && !newUrl.startsWith("https://"))
            newUrl = url.substring(0, url.lastIndexOf("/") + 1) + newUrl;
        URL obj2 = new URL(newUrl);
        con = (HttpURLConnection) obj2.openConnection();
        con.setRequestProperty("Cookie", cookies);
        con.setRequestProperty("User-Agent", "SwaggerServlets");
        if (authorization != null)
            con.setRequestProperty("Authorization", authorization);
        con.setRequestMethod("GET");
        responseCode = con.getResponseCode();
    }
    if (responseCode >= 400 && responseCode < 600) {
        SwaggerError err = null;
        InputStream errorStream = null;
        String string = null;
        errorStream = con.getErrorStream();
        if (errorStream != null)
            string = convertStreamToString(errorStream);
        String errormsg = "HTTP ERROR: " + Integer.toString(responseCode) + " - " + string;
        if (con.getResponseMessage() != null)
            errormsg = errormsg + " - " + con.getResponseMessage();
        errormsg = errormsg.replaceAll("\\s+", " ");
        throw new PresentableUnloggedException(errormsg);
    }
    FetchResponse resp = new FetchResponse();
    resp.html = convertStreamToString(con.getInputStream());
    resp.headerFields = con.getHeaderFields();
    return resp;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) SwaggerError(com.crivano.swaggerservlet.SwaggerError) DataOutputStream(java.io.DataOutputStream) InputStream(java.io.InputStream) PresentableUnloggedException(com.crivano.swaggerservlet.PresentableUnloggedException) URL(java.net.URL)

Aggregations

PresentableUnloggedException (com.crivano.swaggerservlet.PresentableUnloggedException)1 SwaggerError (com.crivano.swaggerservlet.SwaggerError)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1