1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.client.core;
18  
19  import java.io.IOException;
20  import java.util.Enumeration;
21  import java.util.Iterator;
22  import java.util.StringTokenizer;
23  import javax.servlet.ServletConfig;
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServlet;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import javax.xml.soap.MessageFactory;
29  import javax.xml.soap.MimeHeader;
30  import javax.xml.soap.MimeHeaders;
31  import javax.xml.soap.SOAPException;
32  import javax.xml.soap.SOAPMessage;
33  
34  import org.springframework.util.StringUtils;
35  
36  /**
37   * A simple Servlet that uses SAAJ to echo request.
38   *
39   * @author Arjen Poutsma
40   * @since 1.0.0
41   */
42  public class SimpleSaajServlet extends HttpServlet {
43  
44      private MessageFactory msgFactory = null;
45  
46      public void init(ServletConfig servletConfig) throws ServletException {
47          super.init(servletConfig);
48          try {
49              msgFactory = MessageFactory.newInstance();
50          }
51          catch (SOAPException ex) {
52              throw new ServletException("Unable to create message factory" + ex.getMessage());
53          }
54      }
55  
56      private MimeHeaders getHeaders(HttpServletRequest httpServletRequest) {
57          Enumeration enumeration = httpServletRequest.getHeaderNames();
58          MimeHeaders headers = new MimeHeaders();
59          while (enumeration.hasMoreElements()) {
60              String headerName = (String) enumeration.nextElement();
61              String headerValue = httpServletRequest.getHeader(headerName);
62              StringTokenizer values = new StringTokenizer(headerValue, ",");
63              while (values.hasMoreTokens()) {
64                  headers.addHeader(headerName, values.nextToken().trim());
65              }
66          }
67          return headers;
68      }
69  
70      private void putHeaders(MimeHeaders headers, HttpServletResponse res) {
71          Iterator it = headers.getAllHeaders();
72          while (it.hasNext()) {
73              MimeHeader header = (MimeHeader) it.next();
74              String[] values = headers.getHeader(header.getName());
75              String value = StringUtils.arrayToCommaDelimitedString(values);
76              res.setHeader(header.getName(), value);
77          }
78      }
79  
80      public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
81          try {
82              MimeHeaders headers = getHeaders(req);
83              SOAPMessage request = msgFactory.createMessage(headers, req.getInputStream());
84              SOAPMessage reply = onMessage(request);
85              if (reply != null) {
86                  if (reply.saveRequired()) {
87                      reply.saveChanges();
88                  }
89                  resp.setStatus(!reply.getSOAPBody().hasFault() ? HttpServletResponse.SC_OK :
90                          HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
91                  putHeaders(reply.getMimeHeaders(), resp);
92                  reply.writeTo(resp.getOutputStream());
93              }
94              else {
95                  resp.setStatus(HttpServletResponse.SC_ACCEPTED);
96              }
97          }
98          catch (Exception ex) {
99              throw new ServletException("SAAJ POST failed " + ex.getMessage());
100         }
101     }
102 
103     protected SOAPMessage onMessage(SOAPMessage message) {
104         return message;
105     }
106 
107 
108 }