Example usage for java.net ProtocolException getMessage

List of usage examples for java.net ProtocolException getMessage

Introduction

In this page you can find the example usage for java.net ProtocolException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.hpe.application.automation.tools.srf.run.RunFromSrfBuilder.java

private void fillExecutionReqBody() throws IOException, SrfException {
    _con.setDoOutput(true);/*from  w  w w .j  ava  2 s .c  o  m*/
    JSONObject data = new JSONObject();
    JSONObject testParams = new JSONObject();
    JSONObject ciParameters = new JSONObject();
    if (srfTestId != null && srfTestId.length() > 0) {
        data.put("testYac", applyJobParams(srfTestId));
    } else if (srfTagNames != null && !srfTagNames.isEmpty()) {
        String[] tagNames = normalizeTags();
        data.put("tags", tagNames);
    } else
        throw new SrfException("Both test id and test tags are empty");

    if (srfTunnelName != null && srfTunnelName.length() > 0) {
        data.put("tunnelName", srfTunnelName);
    }

    if (data.size() == 0) {
        throw new IOException("Wrong filter");
    }

    testParams.put("filter", data);
    Properties ciProps = new Properties();
    Properties props = new Properties();
    String buildNumber = applyJobParams(srfBuildNumber);
    String releaseNumber = applyJobParams(srfReleaseNumber);
    if (buildNumber != null && buildNumber.length() > 0) {
        data.put("build", buildNumber);
    }
    if (releaseNumber != null && releaseNumber.length() > 0)
        data.put("release", releaseNumber);

    this.logger.print(String.format("Required build & release: %1s %2s\n\r", buildNumber, releaseNumber));
    HashMap<String, String> paramObj = new HashMap<String, String>();
    int cnt = 0;

    if (srfTestParameters != null && !srfTestParameters.isEmpty()) {
        cnt = srfTestParameters.size();
        if (cnt > 0)
            logger.print("Parameters: \n\r");
        for (int i = 0; i < cnt; i++) {
            String name = srfTestParameters.get(i).getName();
            String val = applyJobParams(srfTestParameters.get(i).getValue());
            paramObj.put(name, val);
            logger.print(String.format("%1s : %2s\n\r", name, val));
        }
    }

    if (cnt > 0)
        data.put("params", paramObj);
    //add request header

    //     con.setRequestProperty("session-context", context);
    try {
        OutputStream out = _con.getOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(out);
        writer.write(data.toString());
        writer.flush();
        out.flush();
        out.close();
    } catch (ProtocolException e) {
        logger.print(e.getMessage());
        logger.print("\n\r");
    }
}

From source file:dk.clarin.tools.workflow.java

/**
* Sends an HTTP GET request to a url/*from   w w w.j  av a2s  .c  om*/
*
* @param endpoint - The URL of the server. (Example: " http://www.yahoo.com/search")
* @param requestString - all the request parameters (Example: "param1=val1&param2=val2"). Note: This method will add the question mark (?) to the request - DO NOT add it yourself
* @return - The response from the end point
*/

public static int sendRequest(String result, String endpoint, String requestString, bracmat BracMat,
        String filename, String jobID, boolean postmethod) {
    int code = 0;
    String message = "";
    //String filelist;
    if (endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
        // Send a GET or POST request to the servlet
        try {
            // Construct data
            String requestResult = "";
            String urlStr = endpoint;
            if (postmethod) // HTTP POST
            {
                logger.debug("HTTP POST");
                StringReader input = new StringReader(requestString);
                StringWriter output = new StringWriter();
                URL endp = new URL(endpoint);

                HttpURLConnection urlc = null;
                try {
                    urlc = (HttpURLConnection) endp.openConnection();
                    try {
                        urlc.setRequestMethod("POST");
                    } catch (ProtocolException e) {
                        throw new Exception("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
                    }
                    urlc.setDoOutput(true);
                    urlc.setDoInput(true);
                    urlc.setUseCaches(false);
                    urlc.setAllowUserInteraction(false);
                    urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");

                    OutputStream out = urlc.getOutputStream();

                    try {
                        Writer writer = new OutputStreamWriter(out, "UTF-8");
                        pipe(input, writer);
                        writer.close();
                    } catch (IOException e) {
                        throw new Exception("IOException while posting data", e);
                    } finally {
                        if (out != null)
                            out.close();
                    }
                } catch (IOException e) {
                    throw new Exception("Connection error (is server running at " + endp + " ?): " + e);
                } finally {
                    if (urlc != null) {
                        code = urlc.getResponseCode();

                        if (code == 200) {
                            got200(result, BracMat, filename, jobID, urlc.getInputStream());
                        } else {
                            InputStream in = urlc.getInputStream();
                            try {
                                Reader reader = new InputStreamReader(in);
                                pipe(reader, output);
                                reader.close();
                                requestResult = output.toString();
                            } catch (IOException e) {
                                throw new Exception("IOException while reading response", e);
                            } finally {
                                if (in != null)
                                    in.close();
                            }
                            logger.debug("urlc.getResponseCode() == {}", code);
                            message = urlc.getResponseMessage();
                            didnotget200(code, result, endpoint, requestString, BracMat, filename, jobID,
                                    postmethod, urlStr, message, requestResult);
                        }
                        urlc.disconnect();
                    } else {
                        code = 0;
                        didnotget200(code, result, endpoint, requestString, BracMat, filename, jobID,
                                postmethod, urlStr, message, requestResult);
                    }
                }
                //requestResult = output.toString();
                logger.debug("postData returns " + requestResult);
            } else // HTTP GET
            {
                logger.debug("HTTP GET");
                // Send data

                if (requestString != null && requestString.length() > 0) {
                    urlStr += "?" + requestString;
                }
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection();
                conn.connect();

                // Cast to a HttpURLConnection
                if (conn instanceof HttpURLConnection) {
                    HttpURLConnection httpConnection = (HttpURLConnection) conn;
                    code = httpConnection.getResponseCode();
                    logger.debug("httpConnection.getResponseCode() == {}", code);
                    message = httpConnection.getResponseMessage();
                    BufferedReader rd;
                    StringBuilder sb = new StringBuilder();
                    ;
                    //String line;
                    if (code == 200) {
                        got200(result, BracMat, filename, jobID, httpConnection.getInputStream());
                    } else {
                        // Get the error response
                        rd = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
                        int nextChar;
                        while ((nextChar = rd.read()) != -1) {
                            sb.append((char) nextChar);
                        }
                        rd.close();
                        requestResult = sb.toString();
                        didnotget200(code, result, endpoint, requestString, BracMat, filename, jobID,
                                postmethod, urlStr, message, requestResult);
                    }
                } else {
                    code = 0;
                    didnotget200(code, result, endpoint, requestString, BracMat, filename, jobID, postmethod,
                            urlStr, message, requestResult);
                }
            }
            logger.debug("Job " + jobID + " receives status code [" + code + "] from tool.");
        } catch (Exception e) {
            //jobs = 0; // No more jobs to process now, probably the tool is not reachable
            logger.warn("Job " + jobID + " aborted. Reason:" + e.getMessage());
            /*filelist =*/ BracMat.Eval("abortJob$(" + result + "." + jobID + ")");
        }
    } else {
        //jobs = 0; // No more jobs to process now, probably the tool is not integrated at all
        logger.warn("Job " + jobID + " aborted. Endpoint must start with 'http://' or 'https://'. (" + endpoint
                + ")");
        /*filelist =*/ BracMat.Eval("abortJob$(" + result + "." + jobID + ")");
    }
    return code;
}