Code example for HttpURLConnection

Methods: disconnect, getContentEncoding, getInputStream, getResponseCode, setConnectTimeout, setRequestProperty

0
     * Cleans up any resources remaining after completion. 
     */ 
    private void cleanup() { 
        if (myActiveConnection != null) {
            try { 
                myActiveConnection.disconnect();
            } catch (Exception e) {
                log.log(Level.SEVERE, "Unable to disconnect.", e);
            } 
            myActiveConnection = null;
        } 
 
        if (responseStream != null) {
            try { 
 
                responseStream.close();
            } catch (IOException e) {
                log.log(Level.SEVERE, "Unable to close response stream.", e);
            } 
            responseStream = null;
        } 
    } 
 
 
    /** 
     * Creates a new SAX parser. 
     * 
     * @return The newly created SAX parser. 
     */ 
    private SAXParser createParser() {
        try { 
            return SAXParserFactory.newInstance().newSAXParser();
 
        } catch (ParserConfigurationException e) {
            throw new OsmosisRuntimeException("Unable to create SAX Parser.", e);
        } catch (SAXException e) {
            throw new OsmosisRuntimeException("Unable to create SAX Parser.", e);
        } 
    } 
 
 
    /** 
     * Reads all data from the server and send it to the {@link Sink}. 
     */ 
    public void run() { 
        try { 
        	mySink.initialize(Collections.<String, Object>emptyMap());
        	 
            SAXParser parser = createParser();
            InputStream inputStream =
            	getInputStream(myBaseUrl + "/map?bbox=" + myLeft + "," + myBottom + "," + myRight + "," + myTop);
 
            // First send the Bound down the pipeline 
            mySink.process(new BoundContainer(new Bound(myRight, myLeft, myTop, myBottom, myBaseUrl)));
 
            try { 
                parser.parse(inputStream, new OsmHandler(mySink, true));
            } finally { 
                inputStream.close();
                inputStream = null;
            } 
 
            mySink.complete();
 
        } catch (SAXParseException e) {
            throw new OsmosisRuntimeException( 
                    "Unable to parse xml" 
                    + ".  publicId=(" + e.getPublicId()
                    + "), systemId=(" + e.getSystemId()
                    + "), lineNumber=" + e.getLineNumber()
                    + ", columnNumber=" + e.getColumnNumber() + ".",
                    e);
        } catch (SAXException e) {
            throw new OsmosisRuntimeException("Unable to parse XML.", e);
        } catch (IOException e) {
            throw new OsmosisRuntimeException("Unable to read XML.", e);
        } finally { 
            mySink.release();
 
            cleanup(); 
        } 
    } 
 
    /** 
     * Open a connection to the given url and return a reader on the input 
     * stream from that connection. 
     * 
     * @param pUrlStr 
     *            The exact url to connect to. 
     * @return An reader reading the input stream (servers answer) or 
     *         <code>null</code>. 
     * @throws IOException 
     *             on io-errors 
     */ 
    private InputStream getInputStream(final String pUrlStr) throws IOException {
        URL url;
        int responseCode;
        String encoding;
 
        url = new URL(pUrlStr);
        myActiveConnection = (HttpURLConnection) url.openConnection();
 
        myActiveConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
 
        responseCode = myActiveConnection.getResponseCode();
 
        if (responseCode != RESPONSECODE_OK) {
            String message;
            String apiErrorMessage;
 
            apiErrorMessage = myActiveConnection.getHeaderField("Error");
 
            if (apiErrorMessage != null) {
                message = "Received API HTTP response code " + responseCode
                + " with message \"" + apiErrorMessage
                + "\" for URL \"" + pUrlStr + "\".";
            } else { 
                message = "Received API HTTP response code " + responseCode
                + " for URL \"" + pUrlStr + "\".";
            } 
 
            throw new OsmosisRuntimeException(message);
        } 
 
        myActiveConnection.setConnectTimeout(TIMEOUT);
 
        encoding = myActiveConnection.getContentEncoding();
 
        responseStream = myActiveConnection.getInputStream();
        if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
            responseStream = new MultiMemberGZIPInputStream(responseStream);
        } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
            responseStream = new InflaterInputStream(responseStream, new Inflater(true));
        }