001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.mex.client;
037:
038: import java.io.BufferedReader;
039: import java.io.IOException;
040: import java.io.InputStream;
041: import java.io.InputStreamReader;
042: import java.io.OutputStreamWriter;
043: import java.io.Writer;
044:
045: import java.net.HttpURLConnection;
046: import java.net.URL;
047: import java.util.logging.Logger;
048:
049: import javax.net.ssl.HostnameVerifier;
050: import javax.net.ssl.HttpsURLConnection;
051: import javax.net.ssl.SSLSession;
052:
053: import com.sun.xml.ws.mex.MessagesMessages;
054:
055: import static com.sun.xml.ws.mex.MetadataConstants.ERROR_LOG_LEVEL;
056: import static com.sun.xml.ws.mex.MetadataConstants.GET_REQUEST;
057:
058: /**
059: * Class that handles making the HTTP POST request
060: * to a service.
061: */
062: public class HttpPoster {
063:
064: private static final Logger logger = Logger
065: .getLogger(HttpPoster.class.getName());
066:
067: /**
068: * Makes the request to the service. It is expected that this
069: * method may throw IOException several times before metadata
070: * is returned successfully.
071: *
072: * @param request A String containing the xml that
073: * will be the payload of the message.
074: * @param address Address of the service.
075: * @return The java.io.InputStream returned by the http
076: * url connection.
077: */
078: InputStream post(final String request, final String address,
079: final String contentType) throws IOException {
080:
081: final URL url = new URL(address);
082: final HttpURLConnection conn = createConnection(url);
083: conn.setDoOutput(true);
084: conn.setDoInput(true);
085: conn.setRequestMethod("POST");
086: conn.setRequestProperty("Content-Type", contentType);
087: conn.setRequestProperty("SOAPAction", GET_REQUEST);
088:
089: final Writer writer = new OutputStreamWriter(conn
090: .getOutputStream());
091: writer.write(request);
092: writer.flush();
093:
094: try {
095: return conn.getInputStream();
096: } catch (IOException ioe) {
097: outputErrorStream(conn);
098:
099: // this exception is caught within the mex code and is logged there
100: throw ioe;
101: } finally {
102: writer.close();
103: }
104: }
105:
106: // This method is simply for debugging/error output
107: private void outputErrorStream(final HttpURLConnection conn) {
108: final InputStream error = conn.getErrorStream();
109: if (error != null) {
110: final BufferedReader reader = new BufferedReader(
111: new InputStreamReader(error));
112: try {
113: if (logger.isLoggable(ERROR_LOG_LEVEL)) {
114: logger.log(ERROR_LOG_LEVEL, MessagesMessages
115: .MEX_0010_ERROR_FROM_SERVER());
116: String line = reader.readLine();
117: while (line != null) {
118: logger.log(ERROR_LOG_LEVEL, line);
119: line = reader.readLine();
120: }
121: logger.log(ERROR_LOG_LEVEL, MessagesMessages
122: .MEX_0011_ERROR_FROM_SERVER_END());
123: }
124: } catch (IOException ioe) {
125: // This exception has no more impact.
126: logger.log(ERROR_LOG_LEVEL, MessagesMessages
127: .MEX_0012_READING_ERROR_STREAM_FAILURE(), ioe);
128: } finally {
129: try {
130: reader.close();
131: } catch (IOException ex) {
132: // This exception has no more impact.
133: logger.log(ERROR_LOG_LEVEL, MessagesMessages
134: .MEX_0013_CLOSING_ERROR_STREAM_FAILURE(),
135: ex);
136: }
137: }
138: }
139: }
140:
141: /**
142: * This method is called by ServiceDescriptorImpl when a
143: * metadata response contains a mex location element. The
144: * location element contains an address of a metadata document
145: * that can be retrieved with an HTTP GET call.
146: *
147: * @param address The address of the document.
148: * @return The java.io.InputStream returned by the http
149: * url connection.
150: */
151: public InputStream makeGetCall(final String address)
152: throws IOException {
153: final URL url = new URL(address);
154: final HttpURLConnection conn = createConnection(url);
155: conn.setRequestMethod("GET");
156: conn.setRequestProperty("Content-Type",
157: "application/x-www-form-urlencoded"); // taken from wsimport
158: try {
159: return conn.getInputStream();
160: } catch (IOException ioe) {
161: outputErrorStream(conn);
162:
163: // this exception is caught within the mex code and is logged there
164: throw ioe;
165: }
166: }
167:
168: /*
169: * This method creates an http url connection and sets the
170: * hostname verifier on it if it's an ssl connection.
171: */
172: private HttpURLConnection createConnection(final URL url)
173: throws IOException {
174:
175: return (HttpURLConnection) url.openConnection();
176: }
177:
178: }
|