Search in sources :

Example 51 with URLConnection

use of java.net.URLConnection in project j2objc by google.

the class LSSerializerImpl method writeToURI.

/** 
     * Serializes the specified node to the specified URI and returns true if the Node 
     * was successfully serialized. 
     * 
     * @see org.w3c.dom.ls.LSSerializer#writeToURI(org.w3c.dom.Node, String)
     * @since DOM Level 3
     * @param nodeArg The Node to serialize.
     * @throws org.w3c.dom.ls.LSException SERIALIZE_ERR: Raised if the 
     * LSSerializer was unable to serialize the node.
     *      
     */
public boolean writeToURI(Node nodeArg, String uri) throws LSException {
    // If nodeArg is null, return false.  Should we throw and LSException instead?
    if (nodeArg == null) {
        return false;
    }
    // Obtain a reference to the serializer to use
    Serializer serializer = fXMLSerializer;
    serializer.reset();
    if (nodeArg != fVisitedNode) {
        // Determine the XML Document version of the Node 
        String xmlVersion = getXMLVersion(nodeArg);
        // Determine the encoding: 1.LSOutput.encoding,
        // 2.Document.inputEncoding, 3.Document.xmlEncoding.
        fEncoding = getInputEncoding(nodeArg);
        if (fEncoding == null) {
            fEncoding = fEncoding != null ? fEncoding : getXMLEncoding(nodeArg) == null ? "UTF-8" : getXMLEncoding(nodeArg);
        }
        serializer.getOutputFormat().setProperty("version", xmlVersion);
        // Set the output encoding and xml version properties
        fDOMConfigProperties.setProperty(DOMConstants.S_XERCES_PROPERTIES_NS + DOMConstants.S_XML_VERSION, xmlVersion);
        fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_ENCODING, fEncoding);
        // serialized.
        if ((nodeArg.getNodeType() != Node.DOCUMENT_NODE || nodeArg.getNodeType() != Node.ELEMENT_NODE || nodeArg.getNodeType() != Node.ENTITY_NODE) && ((fFeatures & XMLDECL) != 0)) {
            fDOMConfigProperties.setProperty(DOMConstants.S_XSL_OUTPUT_OMIT_XML_DECL, DOMConstants.DOM3_DEFAULT_FALSE);
        }
        fVisitedNode = nodeArg;
    }
    // Update the serializer properties
    fXMLSerializer.setOutputFormat(fDOMConfigProperties);
    // 
    try {
        // "unsupported-encoding" fatal error is raised. ??
        if (uri == null) {
            String msg = Utils.messages.createMessage(MsgKey.ER_NO_OUTPUT_SPECIFIED, null);
            if (fDOMErrorHandler != null) {
                fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, msg, MsgKey.ER_NO_OUTPUT_SPECIFIED));
            }
            throw new LSException(LSException.SERIALIZE_ERR, msg);
        } else {
            // REVISIT: Can this be used to get an absolute expanded URI
            String absoluteURI = SystemIDResolver.getAbsoluteURI(uri);
            URL url = new URL(absoluteURI);
            OutputStream urlOutStream = null;
            String protocol = url.getProtocol();
            String host = url.getHost();
            // (e.g., allow "HTTP" as well as "http").
            if (protocol.equalsIgnoreCase("file") && (host == null || host.length() == 0 || host.equals("localhost"))) {
                // do we also need to check for host.equals(hostname)
                urlOutStream = new FileOutputStream(getPathWithoutEscapes(url.getPath()));
            } else {
                // This should support URL's whose schemes are mentioned in
                // RFC1738 other than file
                URLConnection urlCon = url.openConnection();
                urlCon.setDoInput(false);
                urlCon.setDoOutput(true);
                urlCon.setUseCaches(false);
                urlCon.setAllowUserInteraction(false);
                // When writing to a HTTP URI, a HTTP PUT is performed.
                if (urlCon instanceof HttpURLConnection) {
                    HttpURLConnection httpCon = (HttpURLConnection) urlCon;
                    httpCon.setRequestMethod("PUT");
                }
                urlOutStream = urlCon.getOutputStream();
            }
            // set the OutputStream to that obtained from the systemId
            serializer.setOutputStream(urlOutStream);
        }
        // Use this hack till Xalan support JAXP1.3
        if (fDOMSerializer == null) {
            fDOMSerializer = (DOM3Serializer) serializer.asDOM3Serializer();
        }
        // Set the error handler on the DOM3Serializer interface implementation
        if (fDOMErrorHandler != null) {
            fDOMSerializer.setErrorHandler(fDOMErrorHandler);
        }
        // Set the filter on the DOM3Serializer interface implementation
        if (fSerializerFilter != null) {
            fDOMSerializer.setNodeFilter(fSerializerFilter);
        }
        // Set the NewLine character to be used
        fDOMSerializer.setNewLine(fEndOfLine.toCharArray());
        // Serializer your DOM, where node is an org.w3c.dom.Node
        // Assuming that Xalan's serializer can serialize any type of DOM
        // node
        fDOMSerializer.serializeDOM3(nodeArg);
    } catch (LSException lse) {
        // Rethrow LSException.
        throw lse;
    } catch (RuntimeException e) {
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    } catch (Exception e) {
        if (fDOMErrorHandler != null) {
            fDOMErrorHandler.handleError(new DOMErrorImpl(DOMError.SEVERITY_FATAL_ERROR, e.getMessage(), null, e));
        }
        throw (LSException) createLSException(LSException.SERIALIZE_ERR, e).fillInStackTrace();
    }
    return true;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) LSException(org.w3c.dom.ls.LSException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) LSException(org.w3c.dom.ls.LSException) DOMException(org.w3c.dom.DOMException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LSSerializer(org.w3c.dom.ls.LSSerializer) Serializer(org.apache.xml.serializer.Serializer) DOM3Serializer(org.apache.xml.serializer.DOM3Serializer)

Example 52 with URLConnection

use of java.net.URLConnection in project j2objc by google.

the class CookiesTest method get.

private Map<String, List<String>> get(MockWebServer server, String path) throws Exception {
    URLConnection connection = server.getUrl(path).openConnection();
    Map<String, List<String>> headers = connection.getHeaderFields();
    connection.getInputStream().close();
    return headers;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 53 with URLConnection

use of java.net.URLConnection in project j2objc by google.

the class URLStreamHandlerFactoryTest method testFirstUseIsCached.

public void testFirstUseIsCached() throws Exception {
    // clear cached protocol handlers if they exist
    factoryField.set(null, null);
    URL.setURLStreamHandlerFactory(oldFactory);
    // creating a connection should use the platform's default stream handler
    URLConnection connection1 = new URL("http://android.com/").openConnection();
    assertFalse(connection1 instanceof Handler.HandlerURLConnection);
    try {
        // set the property and get another connection. The property should not be honored
        System.setProperty("java.protocol.handler.pkgs", getHandlerPackageName());
        URLConnection connection2 = new URL("http://android.com/").openConnection();
        assertFalse(connection2 instanceof Handler.HandlerURLConnection);
    } finally {
        System.clearProperty("java.protocol.handler.pkgs");
    }
}
Also used : URLStreamHandler(java.net.URLStreamHandler) Handler(libcore.java.net.customstreamhandler.http.Handler) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 54 with URLConnection

use of java.net.URLConnection in project j2objc by google.

the class URLStreamHandlerFactoryTest method testCreateURLStreamHandler.

public void testCreateURLStreamHandler() throws Exception {
    TestURLStreamHandlerFactory shf = new TestURLStreamHandlerFactory();
    assertFalse(isCreateURLStreamHandlerCalled);
    URL.setURLStreamHandlerFactory(shf);
    URL url = new URL("http://android.com/");
    URLConnection connection = url.openConnection();
    assertTrue(connection instanceof Handler.HandlerURLConnection);
    try {
        URL.setURLStreamHandlerFactory(shf);
        fail();
    } catch (Error expected) {
    }
    try {
        URL.setURLStreamHandlerFactory(null);
        fail();
    } catch (Error expected) {
    }
}
Also used : URLStreamHandler(java.net.URLStreamHandler) Handler(libcore.java.net.customstreamhandler.http.Handler) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 55 with URLConnection

use of java.net.URLConnection in project siena by mandubian.

the class SelectHandler method request.

private <T extends Response> T request(TreeMap<String, String> parameters, BasicHandler<T> handler) {
    signParams(METHOD, HOST, PATH, awsSecretAccessKey, parameters);
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(false);
        SAXParser parser = factory.newSAXParser();
        URL url = new URL(PROTOCOL + "://" + HOST + PATH);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=" + ENCODING);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        OutputStream os = connection.getOutputStream();
        // System.out.println(PROTOCOL+"://"+HOST+PATH+"?"+query(parameters));
        os.write(query(parameters).getBytes(ENCODING));
        os.close();
        parser.parse(connection.getInputStream(), handler);
        return handler.response;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OutputStream(java.io.OutputStream) SAXParser(javax.xml.parsers.SAXParser) URL(java.net.URL) URLConnection(java.net.URLConnection) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

URLConnection (java.net.URLConnection)950 URL (java.net.URL)654 IOException (java.io.IOException)378 HttpURLConnection (java.net.HttpURLConnection)310 InputStream (java.io.InputStream)279 InputStreamReader (java.io.InputStreamReader)227 BufferedReader (java.io.BufferedReader)208 Test (org.junit.Test)177 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)145 File (java.io.File)95 MalformedURLException (java.net.MalformedURLException)91 MockResponse (okhttp3.mockwebserver.MockResponse)76 BufferedInputStream (java.io.BufferedInputStream)57 JarURLConnection (java.net.JarURLConnection)57 OutputStream (java.io.OutputStream)55 FileOutputStream (java.io.FileOutputStream)52 FileInputStream (java.io.FileInputStream)47 ArrayList (java.util.ArrayList)47 GZIPInputStream (java.util.zip.GZIPInputStream)37 URI (java.net.URI)35