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: */package org.apache.cxf.systest.provider;
19:
20: import java.io.InputStream;
21: import java.io.StringWriter;
22: import java.io.Writer;
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.LogicalHandler;
32: import javax.xml.ws.handler.LogicalMessageContext;
33: import javax.xml.ws.handler.MessageContext;
34:
35: public class TestLogicalHandler implements
36: LogicalHandler<LogicalMessageContext> {
37: public boolean handleMessage(LogicalMessageContext ctx) {
38: try {
39: LogicalMessage msg = ctx.getMessage();
40: Source payload = msg.getPayload();
41: String request = getSourceAsString(payload);
42: // System.out.println(getSourceAsString(payload));
43:
44: // Make sure SOAP handler has changed the value successfully.
45: if (request.indexOf("ServerSOAPHandler") >= 0) {
46: InputStream greetMeInputStream = getClass()
47: .getResourceAsStream(
48: "resources/GreetMeRpcLiteralReqLogical.xml");
49: StreamSource source = new StreamSource();
50: source.setInputStream(greetMeInputStream);
51: msg.setPayload(source);
52: } else if (request.indexOf("TestGreetMeResponse") >= 0) {
53: InputStream greetMeInputStream = getClass()
54: .getResourceAsStream(
55: "resources/GreetMeRpcLiteralRespLogical.xml");
56: StreamSource source = new StreamSource();
57: source.setInputStream(greetMeInputStream);
58: msg.setPayload(source);
59: }
60:
61: } catch (Exception e) {
62: e.printStackTrace();
63: }
64: return true;
65: }
66:
67: public boolean handleFault(LogicalMessageContext ctx) {
68: return true;
69: }
70:
71: public void close(MessageContext arg0) {
72: }
73:
74: public static String getSourceAsString(Source s) throws Exception {
75: Transformer transformer = TransformerFactory.newInstance()
76: .newTransformer();
77: transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
78: "yes");
79: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
80: Writer out = new StringWriter();
81: StreamResult streamResult = new StreamResult();
82: streamResult.setWriter(out);
83: transformer.transform(s, streamResult);
84: return streamResult.getWriter().toString();
85: }
86: }
|