Example usage for java.net HttpURLConnection getResponseMessage

List of usage examples for java.net HttpURLConnection getResponseMessage

Introduction

In this page you can find the example usage for java.net HttpURLConnection getResponseMessage.

Prototype

public String getResponseMessage() throws IOException 

Source Link

Document

Gets the HTTP response message, if any, returned along with the response code from a server.

Usage

From source file:com.iwgame.iwcloud.baidu.task.util.DownloadUtil.java

/**
 * url//from   w  w  w . ja  va  2s  . com
 * @param strUrl
 *            The Url to be downloaded.
 * @param connectTimeout
 *            Connect timeout in milliseconds.
 * @param readTimeout
 *            Read timeout in milliseconds.
 * @param maxFileSize
 *            Max file size in BYTE.
 * @return The file content as byte array.
 */
public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize)
        throws IOException {
    InputStream in = null;
    try {
        URL url = new URL(strUrl); // URL?
        URLConnection ucon = url.openConnection();
        ucon.setConnectTimeout(connectTimeout);
        ucon.setReadTimeout(readTimeout);
        ucon.connect();
        if (ucon.getContentLength() > maxFileSize) {
            String msg = "File " + strUrl + " size [" + ucon.getContentLength()
                    + "] too large, download stoped.";
            logger.error(msg);
            throw new ClientInternalException(msg);
        }
        if (ucon instanceof HttpURLConnection) {
            HttpURLConnection httpCon = (HttpURLConnection) ucon;
            if (httpCon.getResponseCode() > 399) {
                String msg = "Failed to download file " + strUrl + " server response "
                        + httpCon.getResponseMessage();
                logger.error(msg);
                throw new ClientInternalException(msg);
            }
        }
        in = ucon.getInputStream(); // ?
        byte[] byteBuf = new byte[BUFFER_SIZE];
        byte[] ret = null;
        int count, total = 0;
        // ??
        while ((count = in.read(byteBuf, total, BUFFER_SIZE - total)) > 0) {
            total += count;
            if (total + 124 >= BUFFER_SIZE)
                break;
        }
        if (total < BUFFER_SIZE - 124) {
            ret = ArrayUtils.subarray(byteBuf, 0, total);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream(MATERIAL_SIZE);
            count = total;
            total = 0;
            do {
                bos.write(byteBuf, 0, count);
                total += count;
                if (total > maxFileSize) {
                    String msg = "File " + strUrl + " size exceed [" + maxFileSize + "] download stoped.";
                    logger.error(msg);
                    throw new ClientInternalException(msg);
                }
            } while ((count = in.read(byteBuf)) > 0);
            ret = bos.toByteArray();
        }
        if (ret.length < MIN_SIZE) {
            String msg = "File " + strUrl + " size [" + maxFileSize + "] too small.";
            logger.error(msg);
            throw new ClientInternalException(msg);
        }
        return ret;
    } catch (IOException e) {
        String msg = "Failed to download file " + strUrl + " msg=" + e.getMessage();
        logger.error(msg);
        throw e;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            logger.error("Exception while close url - ", e);
            throw e;
        }
    }
}

From source file:io.github.retz.web.JobRequestRouter.java

public static boolean statHTTPFile(String url, String name) {
    String addr = url.replace("files/browse", "files/download") + "%2F" + maybeURLEncode(name);

    HttpURLConnection conn = null;
    try {//from  ww w.j a v a  2 s.c o m
        conn = (HttpURLConnection) new URL(addr).openConnection();
        conn.setRequestMethod("HEAD");
        conn.setDoOutput(false);
        LOG.debug(conn.getResponseMessage());
        return conn.getResponseCode() == 200 || conn.getResponseCode() == 204;
    } catch (IOException e) {
        LOG.debug("Failed to fetch {}: {}", addr, e.toString());
        return false;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:com.grosscommerce.ICEcat.utilities.Downloader.java

public static void download(String urlFrom, String login, String pwd, OutputStream destStream)
        throws Exception {
    try {/*  ww w  .  jav  a2s  . co m*/
        HttpURLConnection uc = prepareConnection(urlFrom, login, pwd);

        uc.connect();

        if (uc.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Error, code: {0}, message {1}",
                    new Object[] { uc.getResponseCode(), uc.getResponseMessage() });

            return;
        }

        BufferedInputStream is = null;

        if ((uc.getContentEncoding() != null && uc.getContentEncoding().toLowerCase().equals("gzip"))
                || uc.getContentType() != null && uc.getContentType().toLowerCase().contains("gzip")) {
            is = new BufferedInputStream(new GZIPInputStream(uc.getInputStream()));

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Will download gzip data from: {0}",
                    urlFrom);
        } else {
            is = new BufferedInputStream(uc.getInputStream());

            Logger.getLogger(Downloader.class.getName()).log(Level.INFO,
                    "Will download not compressed data from:{0}", urlFrom);
        }

        StreamsHelper.copy(is, destStream);
        destStream.flush();

    } catch (Exception ex) {
        Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, "URL: " + urlFrom, ex);

        throw ex;
    }
}

From source file:io.github.retz.web.JobRequestRouter.java

private static String fetchHTTP(String addr, int retry) throws MalformedURLException, IOException {
    LOG.debug("Fetching {}", addr);
    HttpURLConnection conn = null;
    try {//from   ww w .  j ava  2  s. c om
        conn = (HttpURLConnection) new URL(addr).openConnection();
        conn.setRequestMethod("GET");
        conn.setDoOutput(true);
        LOG.debug(conn.getResponseMessage());

    } catch (MalformedURLException e) {
        LOG.error(e.toString());
        throw e;
    } catch (IOException e) {
        LOG.error(e.toString());
        throw e;
    }

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8))) {
        StringBuilder builder = new StringBuilder();
        String line;
        do {
            line = reader.readLine();
            builder.append(line);
        } while (line != null);
        LOG.debug("Fetched {} bytes from {}", builder.toString().length(), addr);
        return builder.toString();

    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        // Somehow this happens even HTTP was correct
        LOG.debug("Cannot fetch file {}: {}", addr, e.toString());
        // Just retry until your stack get stuck; thanks to SO:33340848
        // and to that crappy HttpURLConnection
        if (retry < 0) {
            LOG.error("Retry failed. Last error was: {}", e.toString());
            throw e;
        }
        return fetchHTTP(addr, retry - 1);
    } finally {
        conn.disconnect();
    }

}

From source file:com.michelin.cio.hudson.plugins.qc.QualityCenterUtils.java

/**
 * Checks the Quality Center server URL.
 *//*from  www . j  av a2s  .c o m*/
public static FormValidation checkQcServerURL(String value, Boolean acceptEmpty) {
    String url;
    // Path to the page to check if the server is alive
    String page = "servlet/tdservlet/TDAPI_GeneralWebTreatment";

    // Do will allow empty value?
    if (StringUtils.isBlank(value)) {
        if (!acceptEmpty) {
            return FormValidation.error(Messages.QualityCenter_ServerURLMustBeDefined());
        } else {
            return FormValidation.ok();
        }
    }

    // Does the URL ends with a "/" ? if not, add it
    if (value.lastIndexOf("/") == value.length() - 1) {
        url = value + page;
    } else {
        url = value + "/" + page;
    }

    // Open the connection and perform a HEAD request
    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("HEAD");

        // Check the response code
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            return FormValidation.error(connection.getResponseMessage());
        }
    } catch (MalformedURLException ex) {
        // This is not a valid URL
        return FormValidation.error(Messages.QualityCenter_MalformedServerURL());
    } catch (IOException ex) {
        // Cant open connection to the server
        return FormValidation.error(Messages.QualityCenter_ErrorOpeningServerConnection());
    }

    return FormValidation.ok();
}

From source file:org.apache.hadoop.hdfs.tools.DelegationTokenFetcher.java

/**
 * Cancel a Delegation Token./*  w ww . jav a 2  s. co m*/
 * @param nnAddr the NameNode's address
 * @param tok the token to cancel
 * @throws IOException
 */
static public void cancelDelegationToken(String nnAddr, Token<DelegationTokenIdentifier> tok)
        throws IOException {
    StringBuilder buf = new StringBuilder();
    buf.append(nnAddr);
    buf.append(CancelDelegationTokenServlet.PATH_SPEC);
    buf.append("?");
    buf.append(CancelDelegationTokenServlet.TOKEN);
    buf.append("=");
    buf.append(tok.encodeToUrlString());
    BufferedReader in = null;
    try {
        URL url = new URL(buf.toString());
        SecurityUtil.fetchServiceTicket(url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException("Error cancelling token:" + connection.getResponseMessage());
        }
    } catch (IOException ie) {
        IOUtils.cleanup(LOG, in);
        throw ie;
    }
}

From source file:dev.meng.wikidata.util.http.HttpUtils.java

public static JSONObject queryForJSONResponse(URL url, String encoding)
        throws ProtocolException, IOException, StringConvertionException {
    JSONObject response = null;/* w  w w.  ja va 2  s.c  o m*/

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.connect();

    if (connection.getResponseCode() == 200) {
        response = new JSONObject(StringUtils.inputStreamToString(connection.getInputStream(), encoding));
    } else {
        throw new IOException("Error in opening: " + url + ", " + connection.getResponseCode() + " "
                + connection.getResponseMessage());
    }

    return response;
}

From source file:com.zack6849.alphabot.api.Utils.java

public static String getTitle(String link) {
    String response = "";
    try {//from w  w w .  j  a v a  2 s .  c  om
        HttpURLConnection conn = (HttpURLConnection) new URL(link).openConnection();
        conn.addRequestProperty("User-Agent", USER_AGENT);
        String type = conn.getContentType();
        int length = conn.getContentLength() / 1024;
        response = String.format("HTTP %s: %s", conn.getResponseCode(), conn.getResponseMessage());
        String info;
        if (type.contains("text") || type.contains("application")) {
            Document doc = Jsoup.connect(link).userAgent(USER_AGENT).followRedirects(true).get();
            String title = doc.title() == null || doc.title().isEmpty() ? "No title found!" : doc.title();
            info = String.format("%s - (Content Type: %s Size: %skb)", title, type, length);
            return info;
        }
        info = String.format("Content Type: %s Size: %skb", type, length);
        return info;

    } catch (IOException ex) {
        if (ex.getMessage().contains("UnknownHostException")) {
            return Colors.RED + "Unknown hostname!";
        }
        return response.isEmpty() ? Colors.RED + "An error occured" : response;
    }
}

From source file:hudson.Main.java

/**
 * Run command and send result to {@code ExternalJob} in the {@code external-monitor-job} plugin.
 * Obsoleted by {@code SetExternalBuildResultCommand} but kept here for compatibility.
 *//*from  w w w .ja  v  a 2  s . com*/
public static int remotePost(String[] args) throws Exception {
    String projectName = args[0];

    String home = getHudsonHome();
    if (!home.endsWith("/"))
        home = home + '/'; // make sure it ends with '/'

    // check for authentication info
    String auth = new URL(home).getUserInfo();
    if (auth != null)
        auth = "Basic " + new Base64Encoder().encode(auth.getBytes("UTF-8"));

    {// check if the home is set correctly
        HttpURLConnection con = open(new URL(home));
        if (auth != null)
            con.setRequestProperty("Authorization", auth);
        con.connect();
        if (con.getResponseCode() != 200 || con.getHeaderField("X-Hudson") == null) {
            System.err.println(home + " is not Hudson (" + con.getResponseMessage() + ")");
            return -1;
        }
    }

    URL jobURL = new URL(home + "job/" + Util.encode(projectName).replace("/", "/job/") + "/");

    {// check if the job name is correct
        HttpURLConnection con = open(new URL(jobURL, "acceptBuildResult"));
        if (auth != null)
            con.setRequestProperty("Authorization", auth);
        con.connect();
        if (con.getResponseCode() != 200) {
            System.err.println(jobURL + " is not a valid external job (" + con.getResponseCode() + " "
                    + con.getResponseMessage() + ")");
            return -1;
        }
    }

    // get a crumb to pass the csrf check
    String crumbField = null, crumbValue = null;
    try {
        HttpURLConnection con = open(
                new URL(home + "crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)'"));
        if (auth != null)
            con.setRequestProperty("Authorization", auth);
        String line = IOUtils.readFirstLine(con.getInputStream(), "UTF-8");
        String[] components = line.split(":");
        if (components.length == 2) {
            crumbField = components[0];
            crumbValue = components[1];
        }
    } catch (IOException e) {
        // presumably this Hudson doesn't use CSRF protection
    }

    // write the output to a temporary file first.
    File tmpFile = File.createTempFile("jenkins", "log");
    try {
        int ret;
        try (OutputStream os = Files.newOutputStream(tmpFile.toPath());
                Writer w = new OutputStreamWriter(os, "UTF-8")) {
            w.write("<?xml version='1.1' encoding='UTF-8'?>");
            w.write("<run><log encoding='hexBinary' content-encoding='" + Charset.defaultCharset().name()
                    + "'>");
            w.flush();

            // run the command
            long start = System.currentTimeMillis();

            List<String> cmd = new ArrayList<String>();
            for (int i = 1; i < args.length; i++)
                cmd.add(args[i]);
            Proc proc = new Proc.LocalProc(cmd.toArray(new String[0]), (String[]) null, System.in,
                    new DualOutputStream(System.out, new EncodingStream(os)));

            ret = proc.join();

            w.write("</log><result>" + ret + "</result><duration>" + (System.currentTimeMillis() - start)
                    + "</duration></run>");
        } catch (InvalidPathException e) {
            throw new IOException(e);
        }

        URL location = new URL(jobURL, "postBuildResult");
        while (true) {
            try {
                // start a remote connection
                HttpURLConnection con = open(location);
                if (auth != null)
                    con.setRequestProperty("Authorization", auth);
                if (crumbField != null && crumbValue != null) {
                    con.setRequestProperty(crumbField, crumbValue);
                }
                con.setDoOutput(true);
                // this tells HttpURLConnection not to buffer the whole thing
                con.setFixedLengthStreamingMode((int) tmpFile.length());
                con.connect();
                // send the data
                try (InputStream in = Files.newInputStream(tmpFile.toPath())) {
                    org.apache.commons.io.IOUtils.copy(in, con.getOutputStream());
                } catch (InvalidPathException e) {
                    throw new IOException(e);
                }

                if (con.getResponseCode() != 200) {
                    org.apache.commons.io.IOUtils.copy(con.getErrorStream(), System.err);
                }

                return ret;
            } catch (HttpRetryException e) {
                if (e.getLocation() != null) {
                    // retry with the new location
                    location = new URL(e.getLocation());
                    continue;
                }
                // otherwise failed for reasons beyond us.
                throw e;
            }
        }
    } finally {
        tmpFile.delete();
    }
}

From source file:org.apache.niolex.commons.net.DownloadUtil.java

/**
 * Validate the HTTP code.//from   w ww. j  ava2  s .  c om
 *
 * @param strUrl The Url to be downloaded.
 * @param httpCon The HTTP connection.
 * @throws NetException
 * @throws IOException
 */
public static void validateHttpCode(final String strUrl, HttpURLConnection httpCon)
        throws NetException, IOException {
    if (!IntegerUtil.isIn(httpCon.getResponseCode(), HTTP_OK, HTTP_NOT_AUTHORITATIVE, HTTP_MOVED_PERM,
            HTTP_MOVED_TEMP)) {
        throw new NetException(NetException.ExCode.INVALID_SERVER_RESPONSE,
                "File " + strUrl + " invalid response [" + httpCon.getResponseMessage() + "]");
    }
}