1   package org.mortbay.cxf.demo;
2   //========================================================================
3   //Copyright 2007 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  
17  import javax.servlet.ServletException;
18  import javax.servlet.http.HttpServlet;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  import javax.xml.ws.BindingProvider;
22  import java.io.IOException;
23  import java.io.PrintWriter;
24  import java.util.*;
25  import java.util.concurrent.Future;
26  
27  import ebay.apis.eblbasecomponents.*;
28  
29  public class AggregateSerialCXFServlet extends HttpServlet
30  {
31      ShoppingInterface _shoppingPort;
32      public static final String ITEMS_PARAM="items";
33      
34      
35  
36      public void init() throws ServletException
37      {
38          super.init();
39  
40          try
41          {
42              _shoppingPort = new Shopping().getShopping();
43              BindingProvider bp = (BindingProvider) _shoppingPort;
44  
45              // retrieve the URL stub from the WSDL
46              String ebayURL = (String) bp.getRequestContext().
47                      get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
48  
49              // add eBay-required parameters to the URL
50              String endpointURL = ebayURL + "?callname=FindItems&siteid=0" +
51                      "&appid=JesseMcC-1aff-4c3c-b0be-e8379d036f56" +
52                      "&version=551&requestencoding=SOAP";
53  
54              // replace the endpoint address with the new value
55              bp.getRequestContext().
56                      put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);
57              
58          }
59          catch (Exception e)
60          {
61              System.out.println("Exception: " + e.getMessage());
62              throw new ServletException(e);
63          }
64      }
65  
66      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
67      {
68          doPost(req, resp);
69      }
70  
71      protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
72      {
73          Object itemsObj = req.getParameter(ITEMS_PARAM);
74          List<String> items = new ArrayList<String>();
75  
76          if (itemsObj == null)
77          {
78              resp.setContentType("text/html");
79              PrintWriter out = resp.getWriter();
80  
81              out.println("<HTML><BODY>pass in url with ?items=a,b,c,d<br/>for more dramatic results run multiple times with 10 or more items</BODY></HTML>");
82              out.close();
83  
84          }
85          else
86          {
87              StringTokenizer strtok = new StringTokenizer( (String)itemsObj, ",");
88  
89              while ( strtok.hasMoreTokens() )
90              {
91                  items.add( strtok.nextToken() );
92              }
93          }
94  
95  
96          long _totalTime = System.currentTimeMillis();
97          
98          
99          FindItemsResponseType[] responses=new FindItemsResponseType[items.size()];
100         int r=0;
101         
102         // make all requests serially
103         for (String item: items)
104         {
105             FindItemsRequestType ebayReq = new FindItemsRequestType();
106             ebayReq.setQueryKeywords( item );
107             ebayReq.setMaxEntries(4);
108             responses[r++] = _shoppingPort.findItems(ebayReq);
109         }
110             
111 
112         resp.setContentType( "text/html" );
113         PrintWriter out = resp.getWriter();
114         out.println( "<HTML><BODY>");
115         
116 
117         int i=0;
118         for (String item: items)
119         {
120             FindItemsResponseType response=responses[i];
121             if (response==null)
122             {
123                 out.println(items.get(i)+" MISSING RESPONSE!");
124             }
125             else
126             {
127                 out.print( "<b>" );
128                 out.print( items.get(i) );
129                 out.println( "</b>: " );
130                 String coma=null;
131                 for (SimpleItemType sit : response.getItem())
132                 {
133                     if (coma==null)
134                         coma=", ";
135                     else
136                         out.print(coma);
137                     out.print("<a href=\"");
138                     out.print( sit.getViewItemURLForNaturalSearch());
139                     out.print("\">");
140                     out.print( sit.getTitle());
141                     out.print("</a>");
142                 }
143             }
144             i++;
145             out.println( "<br/>");
146         }
147 
148         out.print( "Total Time: ");
149         long duration=System.currentTimeMillis()-_totalTime;
150         out.print( duration );
151         out.println( "ms<br/>");
152         out.print( "Thread held: ");
153         out.print( duration );
154         out.println( "ms");
155         
156         out.println("</BODY></HTML>" );
157         out.close();   
158         
159         
160 
161     }
162 }