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.xml.transform.OutputKeys;
027: import javax.xml.transform.Source;
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerFactory;
030: import javax.xml.transform.stream.StreamResult;
031: import javax.xml.transform.stream.StreamSource;
032: import javax.xml.ws.LogicalMessage;
033: import javax.xml.ws.ProtocolException;
034: import javax.xml.ws.handler.MessageContext;
035:
036: import org.apache.axis2.jaxws.handler.AttachmentsAdapter;
037: import org.apache.axis2.jaxws.handler.LogicalMessageContext;
038:
039: /*
040: * You can't actually specify whether a handler is for client or server,
041: * you just have to check in the handleMessage and/or handleFault to make
042: * sure what direction we're going.
043: */
044:
045: public class AddNumbersClientLogicalHandler implements
046: javax.xml.ws.handler.LogicalHandler<LogicalMessageContext> {
047:
048: public void close(MessageContext messagecontext) {
049: }
050:
051: public boolean handleFault(LogicalMessageContext messagecontext) {
052: return true;
053: }
054:
055: public boolean handleMessage(LogicalMessageContext messagecontext) {
056: Boolean outbound = (Boolean) messagecontext
057: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
058: if (!outbound) { // inbound response on the client
059:
060: // make sure standard property is available
061:
062: Object bob = messagecontext
063: .get(LogicalMessageContext.HTTP_RESPONSE_CODE);
064: if (bob == null) {
065: throw new NullPointerException("bob is null");
066: }
067:
068: // previously caused a NPE due to internal Properties.putAll(map);
069: // where 'map' had a key/value pair with null value. So, internally
070: // we now use HashMap instead of Properties.
071: int size = messagecontext.size();
072:
073: /*
074: * These props were set on the outbound flow. Inbound flow handlers
075: * should have access to them.
076: */
077: String propKey = "AddNumbersClientProtocolHandlerOutboundAppScopedProperty";
078: String myClientVal = (String) messagecontext.get(propKey);
079: if (myClientVal == null) {
080: throw new RuntimeException(
081: "Property "
082: + propKey
083: + " was null. "
084: + "MEPContext is not searching hard enough for the property.");
085: }
086:
087: // Check for the presences of the attachment property
088: propKey = MessageContext.INBOUND_MESSAGE_ATTACHMENTS;
089: Map map = (Map) messagecontext.get(propKey);
090: if (map == null) {
091: throw new RuntimeException("Property " + propKey
092: + " was null");
093: }
094: if (!(map instanceof AttachmentsAdapter)) {
095: throw new RuntimeException(
096: "Expected AttachmentAddapter for Property "
097: + propKey);
098: }
099: propKey = "AddNumbersClientProtocolHandlerOutboundHandlerScopedProperty";
100: myClientVal = (String) messagecontext.get(propKey);
101: if (myClientVal == null) {
102: throw new RuntimeException(
103: "Property "
104: + propKey
105: + " was null. "
106: + "MEPContext is not searching hard enough for the property.");
107: }
108:
109: /*
110: * These props were set on the inbound flow. Inbound flow handlers
111: * should have access to them.
112: */
113: propKey = "AddNumbersClientProtocolHandlerInboundAppScopedProperty";
114: myClientVal = (String) messagecontext.get(propKey);
115: if (myClientVal == null) {
116: throw new RuntimeException(
117: "Property "
118: + propKey
119: + " was null. "
120: + "MEPContext is not searching hard enough for the property.");
121: }
122: propKey = "AddNumbersClientProtocolHandlerInboundHandlerScopedProperty";
123: myClientVal = (String) messagecontext.get(propKey);
124: if (myClientVal == null) {
125: throw new RuntimeException(
126: "Property "
127: + propKey
128: + " was null. "
129: + "MEPContext is not searching hard enough for the property.");
130: }
131: LogicalMessage msg = messagecontext.getMessage();
132: String st = getStringFromSourcePayload(msg.getPayload());
133: String txt = String.valueOf(Integer
134: .valueOf(getFirstArg(st)) - 1);
135: st = replaceFirstArg(st, txt);
136: msg.setPayload(new StreamSource(
137: new StringBufferInputStream(st)));
138: } else {
139: LogicalMessage msg = messagecontext.getMessage();
140:
141: Source s = msg.getPayload();
142: s = msg.getPayload();
143:
144: String st = getStringFromSourcePayload(msg.getPayload());
145: if (st.contains("<arg0>99</arg0>")) {
146: throw new ProtocolException("I don't like the value 99");
147: }
148:
149: // Check for the presences of the attachment property
150: String propKey = MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS;
151: Map map = (Map) messagecontext.get(propKey);
152: if (map == null) {
153: throw new RuntimeException("Property " + propKey
154: + " was null");
155: }
156: if (!(map instanceof AttachmentsAdapter)) {
157: throw new RuntimeException(
158: "Expected AttachmentAddapter for Property "
159: + propKey);
160: }
161: }
162: return true;
163: }
164:
165: private static String getFirstArg(String payloadString) {
166: StringTokenizer st = new StringTokenizer(payloadString, ">");
167: st.nextToken(); // skip first token.
168: st.nextToken(); // skip second
169: String tempString = st.nextToken();
170: String returnString = new StringTokenizer(tempString, "<")
171: .nextToken();
172: return returnString;
173: }
174:
175: private static String replaceFirstArg(String payloadString,
176: String newArg) {
177: String firstArg = getFirstArg(payloadString);
178: payloadString = payloadString.replaceFirst(firstArg, newArg);
179: return payloadString;
180: }
181:
182: private static String getStringFromSourcePayload(Source payload) {
183: try {
184:
185: TransformerFactory factory = TransformerFactory
186: .newInstance();
187: Transformer trans = factory.newTransformer();
188:
189: ByteArrayOutputStream baos = new ByteArrayOutputStream();
190: StreamResult result = new StreamResult(baos);
191:
192: trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
193: "yes");
194: trans.transform(payload, result);
195:
196: return new String(baos.toByteArray());
197: } catch (Exception e) {
198: throw new RuntimeException(e);
199: }
200:
201: }
202:
203: }
|