001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.http.interceptor;
019:
020: import javax.xml.stream.XMLStreamWriter;
021:
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Node;
024:
025: import org.apache.cxf.binding.http.HttpConstants;
026: import org.apache.cxf.binding.http.URIMapper;
027: import org.apache.cxf.binding.xml.interceptor.XMLMessageOutInterceptor;
028: import org.apache.cxf.endpoint.Endpoint;
029: import org.apache.cxf.helpers.DOMUtils;
030: import org.apache.cxf.interceptor.Fault;
031: import org.apache.cxf.interceptor.InterceptorChain;
032: import org.apache.cxf.interceptor.StaxOutInterceptor;
033: import org.apache.cxf.interceptor.WrappedOutInterceptor;
034: import org.apache.cxf.message.Message;
035: import org.apache.cxf.phase.AbstractPhaseInterceptor;
036: import org.apache.cxf.phase.Phase;
037: import org.apache.cxf.service.model.BindingOperationInfo;
038: import org.apache.cxf.staxutils.W3CDOMStreamWriter;
039:
040: public class DatabindingOutSetupInterceptor extends
041: AbstractPhaseInterceptor<Message> {
042: private static final WrappedOutInterceptor WRAPPED_OUT = new WrappedOutInterceptor();
043: private static final XMLMessageOutInterceptor XML_OUT = new XMLMessageOutInterceptor();
044: private static final StaxOutInterceptor STAX_OUT = new StaxOutInterceptor();
045:
046: public DatabindingOutSetupInterceptor() {
047: super (Phase.PRE_LOGICAL);
048: }
049:
050: public void handleMessage(Message message) throws Fault {
051: boolean client = Boolean.TRUE.equals(message
052: .get(Message.REQUESTOR_ROLE));
053:
054: InterceptorChain chain = message.getInterceptorChain();
055:
056: if (client) {
057: Document document = DOMUtils.createDocument();
058: message.setContent(Node.class, document);
059:
060: XMLStreamWriter writer = new W3CDOMStreamWriter(document);
061: message.setContent(XMLStreamWriter.class, writer);
062:
063: WrappedOutInterceptor wrappedOut = new WrappedOutInterceptor(
064: Phase.PRE_LOGICAL);
065: wrappedOut.addAfter(getId());
066: chain.add(wrappedOut);
067:
068: XMLMessageOutInterceptor xmlOut = new XMLMessageOutInterceptor(
069: Phase.PRE_LOGICAL);
070: xmlOut.addAfter(wrappedOut.getId());
071: chain.add(xmlOut);
072:
073: Endpoint ep = message.getExchange().get(Endpoint.class);
074: URIMapper mapper = (URIMapper) ep.getService().get(
075: URIMapper.class.getName());
076: BindingOperationInfo bop = message.getExchange().get(
077: BindingOperationInfo.class);
078:
079: String verb = mapper.getVerb(bop);
080: message.put(Message.HTTP_REQUEST_METHOD, verb);
081: boolean putOrPost = verb.equals(HttpConstants.POST)
082: || verb.equals(HttpConstants.PUT);
083:
084: if (putOrPost) {
085: chain.doIntercept(message);
086: chain.add(new URIParameterOutInterceptor());
087: chain.add(new DocumentWriterInterceptor());
088: chain.add(STAX_OUT);
089: } else {
090: chain.add(new URIParameterOutInterceptor());
091: }
092:
093: } else {
094: chain.add(STAX_OUT);
095: chain.add(WRAPPED_OUT);
096: chain.add(XML_OUT);
097: }
098: }
099:
100: }
|