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: */package org.apache.cxf.systest.handlers;
019:
020: import java.util.Map;
021:
022: import javax.annotation.PostConstruct;
023: import javax.annotation.Resource;
024: import javax.xml.bind.JAXBContext;
025: import javax.xml.bind.JAXBElement;
026: import javax.xml.bind.JAXBException;
027: import javax.xml.transform.Source;
028: import javax.xml.ws.LogicalMessage;
029: import javax.xml.ws.ProtocolException;
030: import javax.xml.ws.WebServiceException;
031: import javax.xml.ws.handler.LogicalHandler;
032: import javax.xml.ws.handler.LogicalMessageContext;
033: import javax.xml.ws.handler.MessageContext;
034:
035: import org.apache.handlers.types.AddNumbers;
036: import org.apache.handlers.types.AddNumbersResponse;
037: import org.apache.handlers.types.ObjectFactory;
038:
039: /**
040: * handles addition of small numbers.
041: */
042: public class SmallNumberHandler extends TestHandlerBase implements
043: LogicalHandler<LogicalMessageContext> {
044: @Resource(name="handlerResource")
045: String injectedString;
046:
047: public SmallNumberHandler() {
048: this (true);
049: }
050:
051: public SmallNumberHandler(boolean serverSide) {
052: super (serverSide);
053: }
054:
055: public String getHandlerId() {
056: return "SmallNumberHandler" + getId();
057: }
058:
059: public final boolean handleMessage(
060: LogicalMessageContext messageContext) {
061: //System.out.println("LogicalMessageHandler handleMessage called");
062:
063: try {
064: boolean outbound = (Boolean) messageContext
065: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
066:
067: if (outbound) {
068: // get the LogicalMessage from our context
069: LogicalMessage msg = messageContext.getMessage();
070:
071: // check the payload, if its an AddNumbers request, we'll intervene
072: JAXBContext jaxbContext = JAXBContext
073: .newInstance(ObjectFactory.class);
074: Object payload = msg.getPayload(jaxbContext);
075: if (payload instanceof JAXBElement) {
076: payload = ((JAXBElement) payload).getValue();
077: }
078:
079: if (payload instanceof AddNumbers) {
080: AddNumbers req = (AddNumbers) payload;
081:
082: // now, if the arguments are small, let's do the calculation here
083: int a = req.getArg0();
084: int b = req.getArg1();
085:
086: if (isSmall(a) && isSmall(b)) {
087: int answer = a + b;
088:
089: //System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
090: // ok, we've done the calculation, so build the
091: // response and set it as the payload of the message
092:
093: AddNumbersResponse resp = new AddNumbersResponse();
094: resp.setReturn(answer);
095: msg.setPayload(new ObjectFactory()
096: .createAddNumbersResponse(resp),
097: jaxbContext);
098:
099: Source src = msg.getPayload();
100: msg.setPayload(src);
101:
102: payload = msg.getPayload(jaxbContext);
103: if (payload instanceof JAXBElement) {
104: payload = ((JAXBElement) payload)
105: .getValue();
106: }
107:
108: AddNumbersResponse resp2 = (AddNumbersResponse) payload;
109: if (resp2 == resp) {
110: throw new WebServiceException(
111: "Shouldn't be the same object");
112: }
113:
114: // finally, return false, indicating that request
115: // processing stops here and our answer will be
116: // returned to the client
117: return false;
118: }
119: }
120: }
121: return true;
122: } catch (JAXBException ex) {
123: throw new ProtocolException(ex);
124: }
125:
126: }
127:
128: public final boolean handleFault(
129: LogicalMessageContext messageContext) {
130: //System.out.println("LogicalMessageHandler handleFault called");
131: //System.out.println(messageContext);
132:
133: return true;
134: }
135:
136: public void close(MessageContext ctx) {
137: //System.out.println("LogicalHandler close called");
138: }
139:
140: public void init(Map config) {
141: //System.out.println("LogicalHandler init called");
142: }
143:
144: public void destroy() {
145: //System.out.println("LogicalHandler close called");
146: }
147:
148: @PostConstruct
149: public void doPostConstruct() {
150: methodCalled("doPostConstruct");
151: }
152:
153: public String getInjectedString() {
154: return injectedString;
155: }
156:
157: private boolean isSmall(int i) {
158: return i > 0 && i <= 10;
159: }
160: }
|