1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.server.provider;
27
28import com.sun.xml.internal.ws.api.SOAPVersion;
29import com.sun.xml.internal.ws.api.WSBinding;
30import com.sun.xml.internal.ws.api.message.Message;
31import com.sun.xml.internal.ws.api.message.Messages;
32import com.sun.xml.internal.ws.api.message.Packet;
33import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
34import com.sun.xml.internal.ws.encoding.xml.XMLMessage;
35import com.sun.xml.internal.ws.resources.ServerMessages;
36
37import javax.activation.DataSource;
38import javax.xml.transform.Source;
39import javax.xml.ws.Service;
40import javax.xml.ws.WebServiceException;
41import javax.xml.ws.handler.MessageContext;
42import javax.xml.ws.http.HTTPException;
43
44/**
45 * @author Jitendra Kotamraju
46 */
47abstract class XMLProviderArgumentBuilder<T> extends ProviderArgumentsBuilder<T> {
48
49    @Override
50    protected Packet getResponse(Packet request, Exception e, WSDLPort port, WSBinding binding) {
51        Packet response = super.getResponse(request, e, port, binding);
52        if (e instanceof HTTPException) {
53            if (response.supports(MessageContext.HTTP_RESPONSE_CODE)) {
54                response.put(MessageContext.HTTP_RESPONSE_CODE, ((HTTPException)e).getStatusCode());
55            }
56        }
57        return response;
58    }
59
60    static XMLProviderArgumentBuilder createBuilder(ProviderEndpointModel model, WSBinding binding) {
61        if (model.mode == Service.Mode.PAYLOAD) {
62            return new PayloadSource();
63        } else {
64            if(model.datatype==Source.class)
65                return new PayloadSource();
66            if(model.datatype== DataSource.class)
67                return new DataSourceParameter(binding);
68            throw new WebServiceException(ServerMessages.PROVIDER_INVALID_PARAMETER_TYPE(model.implClass,model.datatype));
69        }
70    }
71
72    private static final class PayloadSource extends XMLProviderArgumentBuilder<Source> {
73        public Source getParameter(Packet packet) {
74            return packet.getMessage().readPayloadAsSource();
75        }
76
77        public Message getResponseMessage(Source source) {
78            return Messages.createUsingPayload(source, SOAPVersion.SOAP_11);
79        }
80
81        protected Message getResponseMessage(Exception e) {
82            return XMLMessage.create(e);
83        }
84    }
85
86    private static final class DataSourceParameter extends XMLProviderArgumentBuilder<DataSource> {
87        private final WSBinding binding;
88
89        DataSourceParameter(WSBinding binding) {
90            this.binding = binding;
91        }
92        public DataSource getParameter(Packet packet) {
93            Message msg = packet.getInternalMessage();
94            return (msg instanceof XMLMessage.MessageDataSource)
95                    ? ((XMLMessage.MessageDataSource) msg).getDataSource()
96                    : XMLMessage.getDataSource(msg, binding.getFeatures());
97        }
98
99        public Message getResponseMessage(DataSource ds) {
100            return XMLMessage.create(ds, binding.getFeatures());
101        }
102
103        protected Message getResponseMessage(Exception e) {
104            return XMLMessage.create(e);
105        }
106    }
107
108}
109