Code example for URLConnection

Methods: getInputStream, setConnectTimeout, setReadTimeout, setRequestProperty

0
        URLConnection connection;
        connection = getURLConnection(url);
        if (connection == null) {
            throw new CGException("Cannot create a URLConnection for given URL : " + url);
        } 
        connection.setReadTimeout(getReadTimeout());
        connection.setConnectTimeout(getConnectTimeout());
        connection.setRequestProperty("Connection", "close"); // if http is being used
        InputStream inStream = null;
 
        try { 
            inStream = connection.getInputStream();
            StAXOMBuilder builder = new StAXOMBuilder(inStream);
            OMElement doc = builder.getDocumentElement();
            doc.build();
            return doc;
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Reading as XML failed due to ", e);
            } 
            return readNonXML(url);
        } finally { 
            try { 
                if (inStream != null) {
                    inStream.close();
                } 
            } catch (IOException e) {
                log.warn("Error while closing the input stream to: " + url, e);
            } 
        } 
    } 
 
    private static int getReadTimeout() { 
        return Integer.parseInt(CGUtils.getStringProperty(
                CGConstant.READTIMEOUT, 
                String.valueOf(CGConstant.DEFAULT_READTIMEOUT)));
 
    } 
 
    private static int getConnectTimeout() { 
        return Integer.parseInt(CGUtils.getStringProperty(
                CGConstant.CONNECTTIMEOUT, 
                String.valueOf(CGConstant.DEFAULT_CONNECTTIMEOUT)));
    } 
 
    private static OMNode readNonXML(URL url) throws CGException {
 
        try { 
            // Open a new connection 
            URLConnection newConnection = getURLConnection(url);
            if (newConnection == null) {
                if (log.isDebugEnabled()) {
                    log.debug("Cannot create a URLConnection for given URL : " + url);
                } 
                return null; 
            } 
 
            BufferedInputStream newInputStream = new BufferedInputStream(
                    newConnection.getInputStream());
 
            OMFactory omFactory = OMAbstractFactory.getOMFactory();
            return omFactory.createOMText(
                    new DataHandler(new SynapseBinaryDataSource(newInputStream,
                            newConnection.getContentType())), true);
 
        } catch (IOException e) {
            throw new CGException("Error when getting a stream from resource's content", e);
        } 
    } 
 
    private static URLConnection getURLConnection(URL url) throws CGException {
        URLConnection connection;
        if (url.getProtocol().equalsIgnoreCase("https")) {
            String msg = "Connecting through doesn't support";
            log.error(msg);
            throw new CGException(msg);
        } else { 
            try { 
                connection = url.openConnection();
            } catch (IOException e) {
                throw new CGException("Could not open the URL connection", e);
            } 
        } 
        connection.setReadTimeout(getReadTimeout());
        connection.setConnectTimeout(getConnectTimeout());
        connection.setRequestProperty("Connection", "close"); // if http is being used
        return connection;
    } 
     
    public static OMElement getWSDLElement(String wsdlLocation) throws CGException {
        OMNode wsdNode = getOMElementFromURI(wsdlLocation);