001    /** 
002     * 
003     * Copyright 2005 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.codehaus.activesoap.wsif;
019    
020    import org.apache.wsif.WSIFException;
021    import org.apache.wsif.WSIFMessage;
022    import org.apache.wsif.WSIFPort;
023    import org.apache.wsif.base.WSIFDefaultOperation;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.codehaus.activesoap.MessageExchange;
027    import org.codehaus.activesoap.RestClient;
028    import org.codehaus.activesoap.handler.stax.AnyElementMarshaler;
029    
030    import javax.wsdl.Operation;
031    import javax.wsdl.Output;
032    
033    /**
034     * Represents an operation in <a href="http://ws.apache.org/wsif/">WSIF</a>
035     *
036     * @version $Revision: 1.3 $
037     */
038    public class ASOperation extends WSIFDefaultOperation {
039        private static final transient Log log = LogFactory.getLog(ASOperation.class);
040    
041        private WSIFPort port;
042        private Operation operation;
043        private RestClient client;
044        private AnyElementMarshaler marshaler;
045        private String outputName;
046    
047        public ASOperation(WSIFPort port, Operation operation, RestClient client, AnyElementMarshaler marshaler) {
048            this.port = port;
049            this.operation = operation;
050            this.client = client;
051            this.marshaler = marshaler;
052            Output output = operation.getOutput();
053            if (output != null) {
054                this.outputName = output.getName();
055            }
056        }
057    
058        public void executeInputOnlyOperation(WSIFMessage in) throws WSIFException {
059            try {
060                MessageExchange exchange = createMessageExchange();
061                client.invokeOneWay(exchange, createClientHandler(in));
062            }
063            catch (Exception e) {
064                throw new WSIFException("Failed to process oneway for: " + operation + ". Reason: " + e, e);
065            }
066        }
067    
068        public boolean executeRequestResponseOperation(WSIFMessage in, WSIFMessage out, WSIFMessage fault) throws WSIFException {
069            try {
070                MessageExchange exchange = createMessageExchange();
071                Object inputBody = in.getObjectPart("body");
072                Object output = client.invokeRequestReply(exchange, inputBody);
073                out.setObjectPart("body", output);
074                out.setName(outputName);
075            }
076            catch (Exception e) {
077                log.error("Caught: " + e, e);
078                throw new WSIFException("Failed to process oneway for: " + operation + ". Reason: " + e, e);
079            }
080            return true;
081        }
082    
083        public WSIFPort getWSIFPort() {
084            return port;
085        }
086    
087        protected Operation getOperation() throws Exception {
088            return operation;
089        }
090    
091        protected WSIFHandler createClientHandler(WSIFMessage in) {
092            return new WSIFHandler(in, marshaler);
093        }
094    
095        protected MessageExchange createMessageExchange() {
096            MessageExchange exchange = client.createMessageExchange();
097            return exchange;
098        }
099    
100    }