Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.openejb.server.cxf;
 
 import  org.apache.cxf.Bus;
 import  org.apache.cxf.message.Message;
 import  org.apache.cxf.message.MessageImpl;
 import  org.apache.cxf.security.SecurityContext;
 import  org.apache.cxf.service.model.EndpointInfo;
 import  org.apache.cxf.transport.Conduit;
 import  org.apache.cxf.transport.ConduitInitiator;
 import  org.apache.cxf.transport.Destination;
 import  org.apache.cxf.transport.MessageObserver;
 import  org.apache.cxf.transport.http.AbstractHTTPDestination;
 import  org.apache.cxf.ws.addressing.EndpointReferenceType;
 
 import java.util.List;
 import java.util.Map;
 
 public class HttpDestination extends AbstractHTTPDestination {
     private MessageObserver messageObserver;
     private boolean passSecurityContext = false;
 
     public HttpDestination(Bus bus, ConduitInitiator conduitInitiator, EndpointInfo endpointInfothrows IOException {
         super(busconduitInitiatorendpointInfotrue);
     }
 
     public void setPassSecurityContext(boolean passSecurityContext) {
         this. = passSecurityContext;
     }
 
     public boolean getPassSecurityContext() {
         return this.;
     }
 
     public EndpointInfo getEndpointInfo() {
         return this.;
     }
 
     public void invoke(HttpRequest requestHttpResponse responsethrows Exception {
         MessageImpl message = new MessageImpl();
         message.setContent(InputStream.classrequest.getInputStream());
         message.setDestination(this);
 
         message.put(HttpRequest.classrequest);
         message.put(HttpResponse.classresponse);
 
         final HttpServletRequest servletRequest = (HttpServletRequestrequest.getAttribute(.);
         message.put(.servletRequest);
 
         HttpServletResponse servletResponse = (HttpServletResponserequest.getAttribute(.);
         message.put(.servletResponse);
 
         ServletContext servletContext = (ServletContextrequest.getAttribute(.);
         message.put(.servletContext);
 
         if (this. && servletRequest != null) {
             message.put(SecurityContext.classnew SecurityContext() {
                 public Principal getUserPrincipal() {
                     return servletRequest.getUserPrincipal();
                 }
 
                 public boolean isUserInRole(String role) {
                     return servletRequest.isUserInRole(role);
                 }
             });
         }
 
        // this calls copyRequestHeaders()
        setHeaders(message);
        message.put(Message.HTTP_REQUEST_METHOD, request.getMethod().toString());
        message.put(Message.PATH_INFO, request.getURI().getPath());
        message.put(Message.QUERY_STRING, request.getURI().getFragment());
        message.put(Message.CONTENT_TYPE, request.getContentType());
        if (servletRequest != null) {
            message.put(Message.ENCODING, getCharacterEncoding(servletRequest.getCharacterEncoding()));
        }
        getMessageObserver().onMessage(message);
    }
    private static String getCharacterEncoding(String encoding) {
        if (encoding != null) {
            encoding = encoding.trim();
            // work around a bug with Jetty which results in the character
            // encoding not being trimmed correctly:
            // http://jira.codehaus.org/browse/JETTY-302
            if (encoding.endsWith("\"")) {
                encoding = encoding.substring(0, encoding.length() - 1);
            }
        }
        return encoding;
    }
    protected void copyRequestHeaders(Message messageMap<StringList<String>> headers) {
        HttpServletRequest servletRequest = (HttpServletRequestmessage.get(.);
        if (servletRequest != null) {
            Enumeration names = servletRequest.getHeaderNames();
            while (names.hasMoreElements()) {
                String name = (Stringnames.nextElement();
                List<StringheaderValues = headers.get(name);
                if (headerValues == null) {
                    headerValues = new ArrayList<String>();
                    headers.put(nameheaderValues);
                }
                Enumeration values = servletRequest.getHeaders(name);
                while (values.hasMoreElements()) {
                    String value = (Stringvalues.nextElement();
                    headerValues.add(value);
                }
            }
        }
    }
    public Logger getLogger() {
        return Logger.getLogger(HttpDestination.class.getName());
    }
    public Conduit getInbuiltBackChannel(Message inMessage) {
        return new BackChannelConduit(nullinMessage);
    }
    public Conduit getBackChannel(Message inMessage, Message partialResponse, EndpointReferenceType addressthrows IOException {
        Conduit backChannel = null;
        if (address == null) {
            backChannel = new BackChannelConduit(addressinMessage);
        } else {
            if (partialResponse != null) {
                // setup the outbound message to for 202 Accepted
                partialResponse.put(Message.RESPONSE_CODE, .);
                backChannel = new BackChannelConduit(addressinMessage);
            } else {
                backChannel = conduitInitiator.getConduit(endpointInfo, address);
                // ensure decoupled back channel input stream is closed
                backChannel.setMessageObserver(new MessageObserver() {
                    public void onMessage(Message m) {
                        if (m.getContentFormats().contains(InputStream.class)) {
                            InputStream is = m.getContent(InputStream.class);
                            try {
                                is.close();
                            } catch (Exception e) {
                                // ignore
                            }
                        }
                    }
                });
            }
        }
        return backChannel;
    }
    public void shutdown() {
    }
    public synchronized MessageObserver getMessageObserver() {
        return ;
    }
    public synchronized void setMessageObserver(MessageObserver messageObserver) {
        this. = messageObserver;
    }
    protected class BackChannelConduit implements Conduit {
        protected Message request;
        protected EndpointReferenceType target;
        BackChannelConduit(EndpointReferenceType target, Message request) {
            this. = target;
            this. = request;
        }
        public void close(Message msgthrows IOException {
            msg.getContent(OutputStream.class).close();
        }

        
Register a message observer for incoming messages.

Parameters:
observer the observer to notify on receipt of incoming
        public void setMessageObserver(MessageObserver observer) {
            // shouldn't be called for a back channel conduit
        }
        public void prepare(Message messagethrows IOException {
            send(message);
        }

        
Send an outbound message, assumed to contain all the name-value mappings of the corresponding input message (if any).

Parameters:
message the message to be sent.
        public void send(Message messagethrows IOException {
            HttpResponse response = .get(HttpResponse.class);
            // handle response headers
            updateResponseHeaders(message);
            Map<StringList<String>> protocolHeaders = getProtocolHeaders(message);
            // set headers of the HTTP response object
            for (Map.Entry<StringList<String>> entry : protocolHeaders.entrySet()) {
                String headerName = entry.getKey();
                String headerValue = getHeaderValue(entry.getValue());
                response.setHeader(headerNameheaderValue);
            }
            message.setContent(OutputStream.classnew WrappedOutputStream(messageresponse));
        }

        

Returns:
the reference associated with the target Destination
        public EndpointReferenceType getTarget() {
            return ;
        }

        
Retreive the back-channel Destination.

Returns:
the backchannel Destination (or null if the backchannel is built-in)
        public Destination getBackChannel() {
            return null;
        }

        
Close the conduit
        public void close() {
        }
    }
    private String getHeaderValue(List<Stringvalues) {
        Iterator iter = values.iterator();
        StringBuffer buf = new StringBuffer();
        while (iter.hasNext()) {
            buf.append(iter.next());
            if (iter.hasNext()) {
                buf.append(", ");
            }
        }
        return buf.toString();
    }
    protected void setContentType(Message messageHttpResponse response) {
        Map<StringList<String>> protocolHeaders = getProtocolHeaders(message);
        if (protocolHeaders == null || !protocolHeaders.containsKey(Message.CONTENT_TYPE)) {
            String ct = (Stringmessage.get(Message.CONTENT_TYPE);
            String enc = (Stringmessage.get(Message.ENCODING);
            if (null != ct) {
                if (enc != null && ct.indexOf("charset=") == -1) {
                    ct = ct + "; charset=" + enc;
                }
                response.setContentType(ct);
            } else if (enc != null) {
                response.setContentType("text/xml; charset=" + enc);
            }
        }
    }
    @SuppressWarnings({"unchecked"})
    private Map<StringList<String>> getProtocolHeaders(Message message) {
        Map<StringList<String>> protocolHeaders = (Map<StringList<String>>) message.get(Message.PROTOCOL_HEADERS);
        return protocolHeaders;
    }
    private class WrappedOutputStream extends OutputStream {
        private Message message;
        private HttpResponse response;
        private OutputStream rawOutputStream;
        WrappedOutputStream(Message messageHttpResponse response) {
            this. = message;
            this. = response;
        }
        public void write(int bthrows IOException {
            flushHeaders();
            this..write(b);
        }
        public void write(byte b[]) throws IOException {
            flushHeaders();
            this..write(b);
        }
        public void write(byte b[], int offint lenthrows IOException {
            flushHeaders();
            this..write(bofflen);
        }
        public void flush() throws IOException {
            flushHeaders();
            this..flush();
        }
        public void close() throws IOException {
            flushHeaders();
            this..close();
        }
        protected void flushHeaders() throws IOException {
            if (this. != null) {
                return;
            }
            // set response code
            Integer i = (Integerthis..get(Message.RESPONSE_CODE);
            if (i != null) {
                this..setStatusCode(i.intValue());
            }
            // set content-type
            setContentType(this.this.);
            this. = this..getOutputStream();
        }
    }
New to GrepCode? Check out our FAQ X