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.greeter_control;
019:
020: import java.util.Iterator;
021: import java.util.ArrayList;
022: import java.util.Map;
023: import java.util.logging.Logger;
024:
025: import javax.annotation.Resource;
026: import javax.annotation.PreDestroy;
027: import javax.annotation.PostConstruct;
028:
029: import javax.jws.WebService;
030: import javax.jws.HandlerChain;
031: import javax.xml.namespace.QName;
032: import javax.xml.soap.SOAPException;
033: import javax.xml.soap.SOAPFactory;
034: import javax.xml.soap.SOAPFault;
035: import javax.xml.ws.WebServiceContext;
036: import javax.xml.ws.handler.MessageContext;
037: import javax.xml.ws.soap.SOAPFaultException;
038:
039: /* serviceName, portName specified in webservices.xml */
040: @WebService(serviceName="SOAPService",portName="SoapPort",endpointInterface="org.apache.greeter_control.Greeter",targetNamespace="http://apache.org/greeter_control")
041: /* two handlers specified in webservices.xml */
042: @HandlerChain(file="handlers.xml")
043: public class GreeterImpl implements Greeter {
044:
045: private static final Logger LOG = Logger
046: .getLogger(GreeterImpl.class.getName());
047:
048: @Resource
049: private WebServiceContext context;
050:
051: @Resource(name="greeting")
052: private String greeting;
053:
054: public WebServiceContext getContext() {
055: return context;
056: }
057:
058: public String greetMe(String me) {
059: LOG.info("Invoking greetMe " + me);
060:
061: LOG.info("WebServiceContext: " + context);
062: LOG.info("Principal: " + context.getUserPrincipal());
063: LOG.info("Context: " + context.getMessageContext());
064:
065: MessageContext ctx = context.getMessageContext();
066: Iterator iter = ctx.entrySet().iterator();
067: while (iter.hasNext()) {
068: Map.Entry entry = (Map.Entry) iter.next();
069: System.out.println("Key: " + entry.getKey());
070: System.out.println("Value: " + entry.getValue());
071: }
072:
073: // just playing around
074:
075: // send foo=BAR header
076: Map responseHeaders = (Map) ctx
077: .get(MessageContext.HTTP_RESPONSE_HEADERS);
078: if (responseHeaders == null) {
079: LOG
080: .info("Can't get MessageContext.HTTP_RESPONSE_HEADERS from context");
081: } else {
082: ArrayList values = new ArrayList();
083: values.add("BAR");
084: responseHeaders.put("foo", values);
085: }
086:
087: return greeting + " " + me;
088: }
089:
090: @PostConstruct
091: public void init() {
092: System.out.println(this + " PostConstruct");
093: }
094:
095: @PreDestroy()
096: public void destroy() {
097: System.out.println(this + " PreDestroy");
098: }
099:
100: public String sayHi() {
101: LOG.info("Invoking sayHi ");
102:
103: SOAPFault fault = null;
104: try {
105: fault = SOAPFactory.newInstance().createFault();
106: fault.setFaultCode(new QName("http://foo", "MyFaultCode"));
107: fault.setFaultString("my error");
108: fault.setFaultActor("my actor");
109: } catch (SOAPException ex) {
110: throw new RuntimeException(ex);
111: }
112:
113: throw new SOAPFaultException(fault);
114: }
115:
116: public void greetMeOneWay(String me) {
117: LOG.info("Invoking greetMeOneWay " + me);
118: }
119:
120: public void pingMe() throws PingMeFault {
121: LOG.info("Invoking pingMe ");
122: throw new PingMeFault("Custom Fault", null);
123: }
124: }
|