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.local;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.istack.Nullable;
041: import com.sun.xml.ws.api.message.Packet;
042: import com.sun.xml.ws.api.server.ServiceDefinition;
043: import com.sun.xml.ws.api.server.WSEndpoint;
044: import com.sun.xml.ws.api.server.WebServiceContextDelegate;
045: import com.sun.xml.ws.transport.http.WSHTTPConnection;
046: import com.sun.xml.ws.util.ByteArrayBuffer;
047:
048: import javax.xml.ws.handler.MessageContext;
049: import java.io.InputStream;
050: import java.io.OutputStream;
051: import java.net.URI;
052: import java.security.Principal;
053: import java.util.Collections;
054: import java.util.HashMap;
055: import java.util.Iterator;
056: import java.util.List;
057: import java.util.Map;
058:
059: /**
060: * {@link WSHTTPConnection} implemented for the local transport.
061: *
062: * @author WS Development Team
063: */
064: final class LocalConnectionImpl extends WSHTTPConnection implements
065: WebServiceContextDelegate {
066:
067: private final Map<String, List<String>> reqHeaders;
068: private Map<String, List<String>> rspHeaders = null;
069: protected int statusCode;
070: private ByteArrayBuffer baos;
071: /**
072: * The address of the endpoint to which this message is sent.
073: */
074: private final URI baseURI;
075: private final ClosedCallback callback;
076:
077: LocalConnectionImpl(URI baseURI, @NotNull
078: Map<String, List<String>> reqHeaders) {
079: this (baseURI, reqHeaders, null);
080: }
081:
082: LocalConnectionImpl(URI baseURI, @NotNull
083: Map<String, List<String>> reqHeaders, @Nullable
084: ClosedCallback callback) {
085: this .baseURI = baseURI;
086: this .reqHeaders = reqHeaders;
087: this .callback = callback;
088: }
089:
090: public @NotNull
091: InputStream getInput() {
092: return baos.newInputStream();
093: }
094:
095: public @NotNull
096: OutputStream getOutput() {
097: baos = new ByteArrayBuffer();
098: return baos;
099: }
100:
101: public String toString() {
102: return baos.toString();
103: }
104:
105: public @NotNull
106: WebServiceContextDelegate getWebServiceContextDelegate() {
107: return this ;
108: }
109:
110: public Principal getUserPrincipal(Packet request) {
111: return null; // not really supported
112: }
113:
114: public boolean isUserInRole(Packet request, String role) {
115: return false; // not really supported
116: }
117:
118: public @NotNull
119: String getEPRAddress(Packet request, WSEndpoint endpoint) {
120: return baseURI.resolve(
121: "?" + endpoint.getPortName().getLocalPart()).toString();
122: }
123:
124: public String getWSDLAddress(@NotNull
125: Packet request, @NotNull
126: WSEndpoint endpoint) {
127: ServiceDefinition sd = endpoint.getServiceDefinition();
128: if (sd != null) {
129: return sd.getPrimary().getURL().toString();
130: } else
131: return null;
132: }
133:
134: @Property(MessageContext.HTTP_REQUEST_METHOD)
135: public @NotNull
136: String getRequestMethod() {
137: return "POST"; // not really supported
138: }
139:
140: @Override
141: public boolean isSecure() {
142: return false; // not really supported
143: }
144:
145: @Property(MessageContext.QUERY_STRING)
146: public String getQueryString() {
147: return null; // not really supported
148: }
149:
150: @Property(MessageContext.PATH_INFO)
151: public String getPathInfo() {
152: return null; // not really supported
153: }
154:
155: @Override
156: @NotNull
157: public String getBaseAddress() {
158: return null; // not really supported
159: }
160:
161: @Property(MessageContext.HTTP_RESPONSE_CODE)
162: public int getStatus() {
163: return statusCode;
164: }
165:
166: public void setStatus(int statusCode) {
167: this .statusCode = statusCode;
168: }
169:
170: @Override
171: @Property({MessageContext.HTTP_RESPONSE_HEADERS,Packet.OUTBOUND_TRANSPORT_HEADERS})
172: public @Nullable
173: Map<String, List<String>> getResponseHeaders() {
174: return rspHeaders;
175: }
176:
177: @Property({MessageContext.HTTP_REQUEST_HEADERS,Packet.INBOUND_TRANSPORT_HEADERS})
178: public @NotNull
179: Map<String, List<String>> getRequestHeaders() {
180: return reqHeaders;
181: }
182:
183: public String getRequestHeader(String headerName) {
184: List<String> values = getRequestHeaders().get(headerName);
185: if (values == null || values.isEmpty())
186: return null;
187: else
188: return values.get(0);
189: }
190:
191: public void setResponseHeaders(Map<String, List<String>> headers) {
192: if (headers == null)
193: // be defensive
194: this .rspHeaders = new HashMap<String, List<String>>();
195: else {
196: this .rspHeaders = new HashMap<String, List<String>>(headers);
197:
198: for (Iterator<String> itr = rspHeaders.keySet().iterator(); itr
199: .hasNext();) {
200: String key = itr.next();
201: if (key.equalsIgnoreCase("Content-Type")
202: || key.equalsIgnoreCase("Content-Length"))
203: itr.remove();
204: }
205: }
206: }
207:
208: public void setContentTypeResponseHeader(@NotNull
209: String value) {
210: if (rspHeaders == null)
211: rspHeaders = new HashMap<String, List<String>>();
212:
213: rspHeaders
214: .put("Content-Type", Collections.singletonList(value));
215: }
216:
217: @Override
218: public void close() {
219: if (!isClosed()) {
220: super .close();
221: if (callback != null) {
222: callback.onClosed();
223: }
224: }
225: }
226:
227: protected PropertyMap getPropertyMap() {
228: return model;
229: }
230:
231: private static final PropertyMap model;
232:
233: static {
234: model = parse(LocalConnectionImpl.class);
235: }
236: }
|