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:
037: package com.sun.xml.ws.transport.http.server;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.net.httpserver.Headers;
041: import com.sun.net.httpserver.HttpExchange;
042: import com.sun.net.httpserver.HttpsExchange;
043: import com.sun.xml.ws.api.message.Packet;
044: import com.sun.xml.ws.api.server.WSEndpoint;
045: import com.sun.xml.ws.api.server.WebServiceContextDelegate;
046: import com.sun.xml.ws.transport.http.HttpAdapter;
047: import com.sun.xml.ws.transport.http.WSHTTPConnection;
048: import com.sun.xml.ws.developer.JAXWSProperties;
049:
050: import javax.xml.ws.handler.MessageContext;
051: import java.io.FilterInputStream;
052: import java.io.FilterOutputStream;
053: import java.io.IOException;
054: import java.io.InputStream;
055: import java.io.OutputStream;
056: import java.net.URI;
057: import java.security.Principal;
058: import java.util.ArrayList;
059: import java.util.List;
060: import java.util.Map;
061:
062: /**
063: * {@link WSHTTPConnection} used with Java SE endpoints. It provides connection
064: * implementation using {@link HttpExchange} object.
065: *
066: * @author Jitendra Kotamraju
067: */
068: final class ServerConnectionImpl extends WSHTTPConnection implements
069: WebServiceContextDelegate {
070:
071: private final HttpExchange httpExchange;
072: private int status;
073: private final HttpAdapter adapter;
074: private boolean outputWritten;
075:
076: public ServerConnectionImpl(@NotNull
077: HttpAdapter adapter, @NotNull
078: HttpExchange httpExchange) {
079: this .adapter = adapter;
080: this .httpExchange = httpExchange;
081: }
082:
083: @Override
084: @Property(value={MessageContext.HTTP_REQUEST_HEADERS,Packet.INBOUND_TRANSPORT_HEADERS})
085: public @NotNull
086: Map<String, List<String>> getRequestHeaders() {
087: return httpExchange.getRequestHeaders();
088: }
089:
090: @Override
091: public String getRequestHeader(String headerName) {
092: return httpExchange.getRequestHeaders().getFirst(headerName);
093: }
094:
095: @Override
096: public void setResponseHeaders(Map<String, List<String>> headers) {
097: Headers r = httpExchange.getResponseHeaders();
098: r.clear();
099: for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
100: String name = entry.getKey();
101: List<String> values = entry.getValue();
102: // ignore headers that interfere with our correct operations
103: if (!name.equalsIgnoreCase("Content-Length")
104: && !name.equalsIgnoreCase("Content-Type")) {
105: r.put(name, new ArrayList<String>(values));
106: }
107: }
108: }
109:
110: @Override
111: @Property({MessageContext.HTTP_RESPONSE_HEADERS,Packet.OUTBOUND_TRANSPORT_HEADERS})
112: public Map<String, List<String>> getResponseHeaders() {
113: return httpExchange.getResponseHeaders();
114: }
115:
116: @Override
117: public void setContentTypeResponseHeader(@NotNull
118: String value) {
119: httpExchange.getResponseHeaders().set("Content-Type", value);
120: }
121:
122: @Override
123: public void setStatus(int status) {
124: this .status = status;
125: }
126:
127: @Override
128: @Property(MessageContext.HTTP_RESPONSE_CODE)
129: public int getStatus() {
130: return status;
131: }
132:
133: public @NotNull
134: InputStream getInput() {
135:
136: // Light weight http server's InputStream.close() throws exception if
137: // all the bytes are not read. Work around until it is fixed.
138: return new FilterInputStream(httpExchange.getRequestBody()) {
139: // Workaround for "SJSXP XMLStreamReader.next() closes stream".
140: boolean closed;
141:
142: @Override
143: public void close() throws IOException {
144: if (!closed) {
145: while (read() != -1)
146: ;
147: super .close();
148: closed = true;
149: }
150: }
151: };
152: }
153:
154: public @NotNull
155: OutputStream getOutput() throws IOException {
156: assert !outputWritten;
157: outputWritten = true;
158:
159: List<String> lenHeader = httpExchange.getResponseHeaders().get(
160: "Content-Length");
161: int length = (lenHeader != null) ? Integer.parseInt(lenHeader
162: .get(0)) : 0;
163: httpExchange.sendResponseHeaders(getStatus(), length);
164:
165: // Light weight http server's OutputStream.close() throws exception if
166: // all the bytes are not read on the client side(StreamMessage on the client
167: // side doesn't read all bytes.
168: return new FilterOutputStream(httpExchange.getResponseBody()) {
169: @Override
170: public void close() throws IOException {
171: try {
172: super .close();
173: } catch (IOException ioe) {
174: // Ignoring purposefully.
175: }
176: }
177: };
178: }
179:
180: public @NotNull
181: WebServiceContextDelegate getWebServiceContextDelegate() {
182: return this ;
183: }
184:
185: public Principal getUserPrincipal(Packet request) {
186: return httpExchange.getPrincipal();
187: }
188:
189: public boolean isUserInRole(Packet request, String role) {
190: return false;
191: }
192:
193: public @NotNull
194: String getEPRAddress(Packet request, WSEndpoint endpoint) {
195: return WSHttpHandler.getRequestAddress(httpExchange);
196: }
197:
198: public String getWSDLAddress(@NotNull
199: Packet request, @NotNull
200: WSEndpoint endpoint) {
201: String eprAddress = getEPRAddress(request, endpoint);
202: if (adapter.getEndpoint().getPort() != null)
203: return eprAddress + "?wsdl";
204: else
205: return null;
206: }
207:
208: @Override
209: public boolean isSecure() {
210: return (httpExchange instanceof HttpsExchange);
211: }
212:
213: @Override
214: @Property(MessageContext.HTTP_REQUEST_METHOD)
215: public @NotNull
216: String getRequestMethod() {
217: return httpExchange.getRequestMethod();
218: }
219:
220: @Override
221: @Property(MessageContext.QUERY_STRING)
222: public String getQueryString() {
223: URI requestUri = httpExchange.getRequestURI();
224: String query = requestUri.getQuery();
225: if (query != null)
226: return query;
227: return null;
228: }
229:
230: @Override
231: @Property(MessageContext.PATH_INFO)
232: public String getPathInfo() {
233: URI requestUri = httpExchange.getRequestURI();
234: String reqPath = requestUri.getPath();
235: String ctxtPath = httpExchange.getHttpContext().getPath();
236: if (reqPath.length() > ctxtPath.length()) {
237: return reqPath.substring(ctxtPath.length());
238: }
239: return null;
240: }
241:
242: @Property(JAXWSProperties.HTTP_EXCHANGE)
243: public HttpExchange getExchange() {
244: return httpExchange;
245: }
246:
247: @Override
248: @NotNull
249: public String getBaseAddress() {
250: return WSHttpHandler.getRequestAddress(httpExchange);
251: }
252:
253: @Override
254: public String getProtocol() {
255: return httpExchange.getProtocol();
256: }
257:
258: @Override
259: public void setContentLengthResponseHeader(int value) {
260: httpExchange.getResponseHeaders().set("Content-Length",
261: "" + value);
262: }
263:
264: protected PropertyMap getPropertyMap() {
265: return model;
266: }
267:
268: private static final PropertyMap model;
269:
270: static {
271: model = parse(ServerConnectionImpl.class);
272: }
273: }
|