01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.jaxws.sample.addnumbershandler;
20:
21: import java.io.ByteArrayInputStream;
22: import java.io.ByteArrayOutputStream;
23:
24: import javax.xml.transform.OutputKeys;
25: import javax.xml.transform.Source;
26: import javax.xml.transform.Transformer;
27: import javax.xml.transform.TransformerFactory;
28: import javax.xml.transform.stream.StreamResult;
29: import javax.xml.transform.stream.StreamSource;
30: import javax.xml.ws.LogicalMessage;
31: import javax.xml.ws.handler.MessageContext;
32:
33: import org.apache.axis2.jaxws.handler.LogicalMessageContext;
34:
35: public class AddNumbersLogicalHandler2 implements
36: javax.xml.ws.handler.LogicalHandler<LogicalMessageContext> {
37:
38: public void close(MessageContext messagecontext) {
39:
40: }
41:
42: public boolean handleFault(LogicalMessageContext messagecontext) {
43: Boolean outbound = (Boolean) messagecontext
44: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
45: if (outbound) { // outbound response if we're on the server
46: LogicalMessage msg = messagecontext.getMessage();
47: String st = getStringFromSourcePayload(msg.getPayload());
48: st = st.replaceFirst("blarg",
49: "AddNumbersLogicalHandler2 was here");
50: st = st.replaceFirst("I don't like the value 99",
51: "AddNumbersLogicalHandler2 was here");
52: msg.setPayload(new StreamSource(new ByteArrayInputStream(st
53: .getBytes())));
54: }
55: return true;
56: }
57:
58: public boolean handleMessage(LogicalMessageContext messagecontext) {
59: return true;
60: }
61:
62: private static String getStringFromSourcePayload(Source payload) {
63: try {
64:
65: TransformerFactory factory = TransformerFactory
66: .newInstance();
67: Transformer trans = factory.newTransformer();
68:
69: ByteArrayOutputStream baos = new ByteArrayOutputStream();
70: StreamResult result = new StreamResult(baos);
71:
72: trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
73: "yes");
74: trans.transform(payload, result);
75:
76: return new String(baos.toByteArray());
77: } catch (Exception e) {
78: throw new RuntimeException(e);
79: }
80: }
81:
82: }
|