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.StringTokenizer;
024:
025: import javax.xml.transform.OutputKeys;
026: import javax.xml.transform.Source;
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.stream.StreamResult;
030: import javax.xml.transform.stream.StreamSource;
031: import javax.xml.ws.LogicalMessage;
032: import javax.xml.ws.handler.MessageContext;
033:
034: import org.apache.axis2.jaxws.handler.LogicalMessageContext;
035:
036: /*
037: * You can't actually specify whether a handler is for client or server,
038: * you just have to check in the handleMessage and/or handleFault to make
039: * sure what direction we're going.
040: */
041:
042: public class AddNumbersClientLogicalHandler2 implements
043: javax.xml.ws.handler.LogicalHandler<LogicalMessageContext> {
044:
045: public void close(MessageContext messagecontext) {
046: }
047:
048: public boolean handleFault(LogicalMessageContext messagecontext) {
049: return true;
050: }
051:
052: public boolean handleMessage(LogicalMessageContext messagecontext) {
053: Boolean outbound = (Boolean) messagecontext
054: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
055: if (outbound) { // outbound request on the client
056: LogicalMessage msg = messagecontext.getMessage();
057: Source payload = msg.getPayload();
058: String st = getStringFromSourcePayload(payload);
059: String txt = String.valueOf(Integer
060: .valueOf(getFirstArg(st)) * 2);
061: st = replaceFirstArg(st, txt);
062: msg.setPayload(new StreamSource(
063: new StringBufferInputStream(st)));
064: }
065: return true;
066: }
067:
068: private static String getFirstArg(String payloadString) {
069: StringTokenizer st = new StringTokenizer(payloadString, ">");
070: st.nextToken(); // skip first token.
071: st.nextToken(); // skip second
072: String tempString = st.nextToken();
073: String returnString = new StringTokenizer(tempString, "<")
074: .nextToken();
075: return returnString;
076: }
077:
078: private static String replaceFirstArg(String payloadString,
079: String newArg) {
080: String firstArg = getFirstArg(payloadString);
081: payloadString = payloadString.replaceFirst(firstArg, newArg);
082: return payloadString;
083: }
084:
085: private static String getStringFromSourcePayload(Source payload) {
086: try {
087:
088: TransformerFactory factory = TransformerFactory
089: .newInstance();
090: Transformer trans = factory.newTransformer();
091:
092: ByteArrayOutputStream baos = new ByteArrayOutputStream();
093: StreamResult result = new StreamResult(baos);
094:
095: trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
096: "yes");
097: trans.transform(payload, result);
098:
099: return new String(baos.toByteArray());
100: } catch (Exception e) {
101: throw new RuntimeException(e);
102: }
103: }
104: }
|