View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.wsat.simple.servlet;
18  
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.annotation.WebServlet;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.xml.ws.BindingProvider;
30  import javax.xml.ws.WebServiceRef;
31  import javax.xml.ws.handler.Handler;
32  
33  import org.jboss.as.quickstarts.wsat.simple.jaxws.RestaurantServiceAT;
34  import org.jboss.as.quickstarts.wsat.simple.jaxws.RestaurantServiceATService;
35  
36  import com.arjuna.mw.wst11.UserTransaction;
37  import com.arjuna.mw.wst11.UserTransactionFactory;
38  import com.arjuna.mw.wst11.client.JaxWSHeaderContextProcessor;
39  
40  /**
41   * <p>
42   * A simple servlet 3 that begins a WS-AtomicTransaction and invokes a Web service. If the call is successful, the transaction
43   * is committed.
44   * </p>
45   * <p/>
46   * <p/>
47   * The servlet is registered and mapped to /WSATSimpleServletClient using the {@linkplain javax.servlet.annotation.WebServlet}
48   * 
49   * @author Paul Robinson (paul.robinson@redhat.com)
50   * @HttpServlet .
51   *              </p>
52   */
53  @WebServlet("/WSATSimpleServletClient")
54  public class WSATSimpleServletClient extends HttpServlet {
55  
56      private static final long serialVersionUID = -8314035702649252239L;
57  
58      @WebServiceRef(value = RestaurantServiceATService.class)
59      private RestaurantServiceAT client;
60  
61      @Override
62      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
63  
64          /*
65           * Add client handler chain
66           */
67          BindingProvider bindingProvider = (BindingProvider) client;
68          @SuppressWarnings("rawtypes")
69          List<Handler> handlers = new ArrayList<Handler>(1);
70          handlers.add(new JaxWSHeaderContextProcessor());
71          bindingProvider.getBinding().setHandlerChain(handlers);
72  
73          /*
74           * Lookup the DNS name of the server from the environment and set the endpoint address on the client.
75           */
76          String openshift = System.getenv("OPENSHIFT_APP_DNS");
77          if (openshift != null) {
78              bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
79                      "http://" + openshift + "/RestaurantServiceAT");
80          }
81  
82          resp.setContentType("text/html");
83          PrintWriter out = resp.getWriter();
84  
85          out.write("<h1>Quickstart: This example demonstrates the deployment of a WS-AT (WS-AtomicTransaction) enabled JAX-WS Web service bundled in a war archive for deployment to *Red Hat JBoss Enterprise Application Platform 6*.</h1>");
86  
87          System.out.println("[CLIENT] Creating a new WS-AT User Transaction");
88          UserTransaction ut = UserTransactionFactory.userTransaction();
89          try {
90              System.out
91                      .println("[CLIENT] Beginning Atomic Transaction (All calls to Web services that support WS-AT wil be included in this transaction)");
92              ut.begin();
93              System.out.println("[CLIENT] invoking makeBooking() on WS");
94              client.makeBooking();
95              System.out.println("[CLIENT] committing Atomic Transaction (This will cause the AT to complete successfully)");
96              ut.commit();
97  
98              out.write("<p><b>Transaction succeeded!</b></p>");
99  
100         } catch (Exception e) {
101             e.printStackTrace();
102 
103             out.write("<p><b>Transaction failed with the following error:</b></p>");
104             out.write("<p><blockquote>");
105             out.write(e.toString());
106             out.write("</blockquote></p>");
107         } finally {
108             rollbackIfActive(ut);
109             client.reset();
110 
111             out.write("<p><i>Go to your JBoss EAP Server console or log to see the detailed result of the transaction.</i></p>");
112         }
113     }
114 
115     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
116         doGet(req, resp);
117     }
118 
119     /**
120      * Utility method for rolling back a transaction if it is currently active.
121      * 
122      * @param ut The User Business Activity to cancel.
123      */
124     private void rollbackIfActive(UserTransaction ut) {
125         try {
126             ut.rollback();
127         } catch (Throwable th2) {
128             // do nothing, not active
129         }
130     }
131 
132 }