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: */
019: package org.apache.axis2.jaxws.sample.addnumbershandler;
020:
021: import java.io.ByteArrayOutputStream;
022: import java.io.StringBufferInputStream;
023: import java.util.Map;
024: import java.util.StringTokenizer;
025:
026: import javax.annotation.PostConstruct;
027: import javax.xml.transform.OutputKeys;
028: import javax.xml.transform.Source;
029: import javax.xml.transform.Transformer;
030: import javax.xml.transform.TransformerFactory;
031: import javax.xml.transform.stream.StreamResult;
032: import javax.xml.transform.stream.StreamSource;
033: import javax.xml.ws.LogicalMessage;
034: import javax.xml.ws.ProtocolException;
035: import javax.xml.ws.handler.MessageContext;
036: import javax.xml.ws.handler.MessageContext.Scope;
037:
038: import org.apache.axis2.jaxws.handler.AttachmentsAdapter;
039: import org.apache.axis2.jaxws.handler.LogicalMessageContext;
040:
041: public class AddNumbersLogicalHandler implements
042: javax.xml.ws.handler.LogicalHandler<LogicalMessageContext> {
043:
044: private int deduction = 1;
045:
046: public void close(MessageContext messagecontext) {
047:
048: }
049:
050: @PostConstruct
051: public void postConstruct() {
052: deduction = 2;
053: }
054:
055: public boolean handleFault(LogicalMessageContext messagecontext) {
056: return true;
057: }
058:
059: /*
060: * this test handleMessage method is obviously not what a customer might write, but it does
061: * the trick for kicking the tires in the handler framework. The AddNumbers service takes two
062: * ints as incoming params, adds them, and returns the sum. This method subtracts 1 from the
063: * first int on the inbound request, and subtracts "deduction" from the int on the outbound
064: * response. So the client app should expect a sum 3 less than a sum with this handler
065: * manipulating the SOAP message.
066: */
067: public boolean handleMessage(LogicalMessageContext messagecontext) {
068: Boolean outbound = (Boolean) messagecontext
069: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
070: if (!outbound) { // inbound request if we're on the server
071: LogicalMessage msg = messagecontext.getMessage();
072: String st = getStringFromSourcePayload(msg.getPayload());
073: if (st.contains("<arg0>99</arg0>")) {
074: throw new ProtocolException("I don't like the value 99");
075: }
076: String txt = String.valueOf(Integer
077: .valueOf(getFirstArg(st)) - 1);
078: st = replaceFirstArg(st, txt);
079: msg.setPayload(new StreamSource(
080: new StringBufferInputStream(st)));
081:
082: messagecontext.put(
083: "AddNumbersLogicalHandlerInboundAppScopedProperty",
084: "blargval");
085: messagecontext.setScope(
086: "AddNumbersLogicalHandlerInboundAppScopedProperty",
087: Scope.APPLICATION);
088: messagecontext
089: .put(
090: "AddNumbersLogicalHandlerInboundHandlerScopedProperty",
091: "blargval");
092:
093: // Check for the presences of the attachment property
094: String propKey = MessageContext.INBOUND_MESSAGE_ATTACHMENTS;
095: Map map = (Map) messagecontext.get(propKey);
096: if (map == null) {
097: throw new RuntimeException("Property " + propKey
098: + " was null");
099: }
100: if (!(map instanceof AttachmentsAdapter)) {
101: throw new RuntimeException(
102: "Expected AttachmentAddapter for Property "
103: + propKey);
104: }
105:
106: } else { // outbound response if we're on the server
107: LogicalMessage msg = messagecontext.getMessage();
108: String st = getStringFromSourcePayload(msg.getPayload());
109: String txt = String.valueOf(Integer
110: .valueOf(getFirstArg(st))
111: - deduction);
112: st = replaceFirstArg(st, txt);
113: msg.setPayload(new StreamSource(
114: new StringBufferInputStream(st)));
115:
116: // Check for the presences of the attachment property
117: String propKey = MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS;
118: Map map = (Map) messagecontext.get(propKey);
119: if (map == null) {
120: throw new RuntimeException("Property " + propKey
121: + " was null");
122: }
123: if (!(map instanceof AttachmentsAdapter)) {
124: throw new RuntimeException(
125: "Expected AttachmentAddapter for Property "
126: + propKey);
127: }
128: }
129: return true;
130: }
131:
132: private static String getFirstArg(String payloadString) {
133: StringTokenizer st = new StringTokenizer(payloadString, ">");
134: st.nextToken(); // skip first token.
135: st.nextToken(); // skip second
136: String tempString = st.nextToken();
137: String returnString = new StringTokenizer(tempString, "<")
138: .nextToken();
139: return returnString;
140: }
141:
142: private static String replaceFirstArg(String payloadString,
143: String newArg) {
144: String firstArg = getFirstArg(payloadString);
145: payloadString = payloadString.replaceFirst(firstArg, newArg);
146: return payloadString;
147: }
148:
149: private static String getStringFromSourcePayload(Source payload) {
150: try {
151:
152: TransformerFactory factory = TransformerFactory
153: .newInstance();
154: Transformer trans = factory.newTransformer();
155:
156: ByteArrayOutputStream baos = new ByteArrayOutputStream();
157: StreamResult result = new StreamResult(baos);
158:
159: trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
160: "yes");
161: trans.transform(payload, result);
162:
163: return new String(baos.toByteArray());
164: } catch (Exception e) {
165: throw new RuntimeException(e);
166: }
167: }
168: }
|