Example usage for java.net URLConnection setReadTimeout

List of usage examples for java.net URLConnection setReadTimeout

Introduction

In this page you can find the example usage for java.net URLConnection setReadTimeout.

Prototype

public void setReadTimeout(int timeout) 

Source Link

Document

Sets the read timeout to a specified timeout, in milliseconds.

Usage

From source file:org.geoserver.wps.spatialstatistics.ppio.GridCoverageURLPPIO.java

@Override
public Object decode(String input) throws Exception {
    WPSInfo wps = geoServer.getService(WPSInfo.class);

    URL url = new URL(input);
    URLConnection conn = url.openConnection();
    conn.setConnectTimeout((int) wps.getConnectionTimeout());
    conn.setReadTimeout((int) wps.getConnectionTimeout());

    return decode(conn.getInputStream());
}

From source file:org.structnetalign.weight.PrecalculatedFatcatWeight.java

private AFPChain load(String id1, String id2, Atom[] ca1, Atom[] ca2)
        throws IOException, StructureException, WeightException {
    URL url = new URL(BASE_URL + "&" + PARAM + "=" + id1 + "&" + PARAM + "=" + id2);
    URLConnection conn = url.openConnection();
    conn.setReadTimeout(TIMEOUT);
    logger.debug("Loading AFPChain from URL " + url);
    String string;/* w ww. ja va 2  s.  c  o  m*/
    try (InputStream is = conn.getInputStream()) {
        string = IOUtils.toString(is, "UTF-8"); // thanks Apache Commons!
    }
    AFPChain afpChain = AFPChainXMLParser.fromXML(string, ca1, ca2);
    if (afpChain == null)
        throw new WeightException("Got null AFPChain for " + uniProtId2, v1, v2, uniProtId1, uniProtId2, true,
                true);
    // now we need to rotate to make the structure match the alignment
    double tmScore = AFPChainScorer.getTMScore(afpChain, ca1, ca2);
    afpChain.setTMScore(tmScore);
    return afpChain;
}

From source file:no.get.cms.plugin.resourcecompressor.ContentLoader.java

public String load(String srcUrl) {
    try {//from www .  j  a v  a  2 s.  co m
        URL url = new URL(srcUrl);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("Accept-Charset", "UTF-8");
        connection.setConnectTimeout(2000);
        connection.setReadTimeout(10000);
        connection.connect();
        InputStream inputStream = connection.getInputStream();
        String charset = extractCharsetFromContentType(connection.getContentType());
        String body = IOUtils.toString(inputStream, charset);
        IOUtils.close(connection);
        return body;
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.kuali.mobility.itnotices.service.ITNoticesServiceImpl.java

private List<ITNotice> getITNoticesFromFeed() {
    List<ITNotice> notices = new ArrayList<ITNotice>();
    SAXBuilder builder = new SAXBuilder();
    Document doc = null;/*from   w ww  . j  a v a 2 s  .  c  om*/
    URL urlObj;
    try {
        urlObj = new URL("http://itnotices.iu.edu/rss.aspx");
        URLConnection urlConnection = urlObj.openConnection();
        urlConnection.setConnectTimeout(5000);
        urlConnection.setReadTimeout(5000);
        urlConnection.connect();
        doc = builder.build(urlConnection.getInputStream());

        if (doc != null) {
            Element root = doc.getRootElement();
            List items = root.getChild("channel").getChildren("item");
            for (Iterator iterator = items.iterator(); iterator.hasNext();) {
                Element item = (Element) iterator.next();
                String services = "";
                List service = item.getChildren("service");
                for (Iterator iterator2 = service.iterator(); iterator2.hasNext();) {
                    Element s = (Element) iterator2.next();
                    services += s.getContent(0).getValue() + ", ";
                }
                if (services.endsWith(", ")) {
                    services = services.substring(0, services.length() - 2);
                }
                ITNotice notice = new ITNotice(item.getChildTextTrim("lastUpdated"),
                        item.getChildTextTrim("noticeType"), item.getChildTextTrim("title"), services,
                        item.getChildTextTrim("message"));
                determineImage(notice);
                notices.add(notice);
            }
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }

    return notices;
}

From source file:com.ijuru.kumva.app.site.FetchSuggestionsTask.java

@Override
protected List<Suggestion> fetch(String term) {
    URL url = dictionary.createSuggestionsURL(term);

    URLConnection connection;
    try {/*from ww  w.  ja  va 2  s. c o m*/
        connection = url.openConnection();
        connection.setConnectTimeout(timeout);
        connection.setReadTimeout(timeout);

        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;

        while ((line = reader.readLine()) != null)
            sb.append(line);

        JSONArray jsonArray = new JSONArray(sb.toString());
        List<Suggestion> suggestions = new ArrayList<Suggestion>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String text = jsonObject.getString("value");
            String lang = jsonObject.getString("lang");
            suggestions.add(new Suggestion(text, lang));
        }

        return suggestions;

    } catch (Exception e) {
        return null;
    }
}

From source file:com.p000ison.dev.simpleclans2.updater.bamboo.BambooBuild.java

private Reader connect(String file) throws IOException {
    URL buildAPIURL = new URL("http", BAMBOO_HOST, 80, file);
    URLConnection connection = buildAPIURL.openConnection();
    connection.setReadTimeout(1000);
    connection.setConnectTimeout(1000);//from  ww  w  . ja  va2  s  .co m
    return new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8"));
}

From source file:org.n52.geolabel.server.resources.PNGResourceV1.java

@GET
@Produces(MEDIA_TYPE)//ww w .  j  av a  2 s.com
@ApiOperation(value = "Returns a GEO label", notes = "Requires metadata/feedback documents as url")
@ApiResponses({ @ApiResponse(code = 400, message = "Error in feedback/metadata document") })
public Response getLabelSVGByURL(@ApiParam("Url to LML document") @QueryParam(Constants.PARAM_LML) URL lmlURL,
        @ApiParam("Url to metadata document") @QueryParam(Constants.PARAM_METADATA) URL metadataURL,
        @ApiParam("Url to feedback document") @QueryParam(Constants.PARAM_FEEDBACK) URL feedbackURL,
        @ApiParam("Desired size of returned PNG") @QueryParam(Constants.PARAM_SIZE) Integer size,
        @ApiParam("use cached labels") @QueryParam(Constants.PARAM_USECACHE) @DefaultValue(Constants.PARAM_USECACHE_DEFAULT) boolean useCache)
        throws IOException {

    Label label = null;
    if (lmlURL != null) {
        URLConnection con = lmlURL.openConnection();
        con.setConnectTimeout(GeoLabelConfig.CONNECT_TIMEOUT);
        con.setReadTimeout(GeoLabelConfig.READ_TIMEOUT);
        label = Label.fromXML(con.getInputStream());
    } else {
        LMLResourceV1 lmlR = this.lmlResource.get();
        label = lmlR.getLabelByURL(metadataURL, feedbackURL, useCache);
    }
    return createLabelPNGResponse(size != null ? size.intValue() : DEFAULT_PNG_SIZE, label);
}

From source file:dlauncher.dialogs.Login.java

private void login_write(String UserName, String Password) {
    Logger.getGlobal().info("Started login process");
    Logger.getGlobal().info("Writing login data..");
    try {//from ww  w. j a v a 2s .c  o m
        url = new URL("http://authserver.mojang.com/authenticate");

        final URLConnection con = url.openConnection();
        con.setConnectTimeout(5000);
        con.setReadTimeout(5000);
        con.setDoOutput(true);
        con.setDoInput(true);

        con.setRequestProperty("Content-Type", "application/json");
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()))) {
            writer.write("{\n" + "  \"agent\": {" + "    \"name\": \"Minecraft\"," + "    \"version\": 1" + "\n"
                    + "  },\n" + "  \"username\": \"" + UserName + "\",\n" + "\n" + "  \"password\": \""
                    + Password + "\",\n" + "}");

            writer.close();
        }
        Logger.getGlobal().info("Succesful written login data");
    } catch (MalformedURLException ex) {
        Logger.getGlobal().warning("Failed Writing login data,");
        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, "", ex);
    } catch (IOException ex) {
        Logger.getGlobal().warning("Failed Writing login data,");
        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, "", ex);
    }
}

From source file:com.enonic.cms.core.http.HTTPService.java

private URLConnection setUpConnection(String address, int timeoutMs, int readTimeoutMs) throws IOException {
    URL url = new URL(address);
    URLConnection urlConn = url.openConnection();
    urlConn.setConnectTimeout(timeoutMs > 0 ? timeoutMs : DEFAULT_CONNECTION_TIMEOUT);
    urlConn.setReadTimeout(readTimeoutMs > 0 ? readTimeoutMs : DEFAULT_READ_TIMEOUT);
    urlConn.setRequestProperty("User-Agent", userAgent);
    String userInfo = url.getUserInfo();
    if (StringUtils.isNotBlank(userInfo)) {
        String userInfoBase64Encoded = new String(Base64.encodeBase64(userInfo.getBytes()));
        urlConn.setRequestProperty("Authorization", "Basic " + userInfoBase64Encoded);
    }/*from   www.j  a  v a  2s.  c  om*/
    return urlConn;

}

From source file:org.opennms.netmgt.provision.detector.simple.client.DominoIIOPClient.java

/**
 * @param hostAddress//from  ww w.  ja  va  2s .c o  m
 * @param port
 * @return
 */
private boolean retrieveIORText(final String hostAddress, final int port, final int timeout)
        throws IOException {
    String IOR = "";
    final URL u = new URL("http://" + hostAddress + ":" + port + "/diiop_ior.txt");
    try {
        final URLConnection conn = u.openConnection();
        conn.setConnectTimeout(timeout);
        conn.setReadTimeout(timeout);
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(conn.getInputStream());
            br = new BufferedReader(isr);
            boolean done = false;
            while (!done) {
                final String line = br.readLine();
                if (line == null) {
                    // end of stream
                    done = true;
                } else {
                    IOR += line;
                    if (IOR.startsWith("IOR:")) {
                        // the IOR does not span a line, so we're done
                        done = true;
                    }
                }
            }
        } finally {
            IOUtils.closeQuietly(br);
            IOUtils.closeQuietly(isr);
        }
    } catch (final SocketException e) {
        LOG.warn("Unable to connect to {}", u, e);
    }
    if (!IOR.startsWith("IOR:"))
        return false;

    return true;
}