View Javadoc

1   //========================================================================
2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
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   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.eclipse.jetty.example.asyncrest;
16  
17  import java.io.BufferedReader;
18  import java.io.IOException;
19  import java.io.InputStreamReader;
20  import java.io.PrintWriter;
21  import java.net.HttpURLConnection;
22  import java.net.URL;
23  import java.util.LinkedList;
24  import java.util.Map;
25  import java.util.Queue;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.eclipse.jetty.util.ajax.JSON;
33  
34  /**
35   * Servlet implementation class SerialRestServlet
36   */
37  public class SerialRestServlet extends AbstractRestServlet
38  {   
39      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
40      {
41          long start = System.nanoTime();
42          
43  
44          String[] keywords=request.getParameter(ITEMS_PARAM).split(",");
45          Queue<Map<String,String>> results = new LinkedList<Map<String,String>>();
46          
47          // make all requests serially
48          for (String itemName : keywords)
49          {
50              URL url = new URL(restURL(itemName));
51              
52              HttpURLConnection connection = (HttpURLConnection)url.openConnection();
53              connection.setRequestMethod("GET");
54              
55              Map query = (Map)JSON.parse(new BufferedReader(new InputStreamReader(connection.getInputStream())));
56              Object[] auctions = (Object[]) query.get("Item");
57              if (auctions != null)
58              {
59                  for (Object o : auctions)
60                      results.add((Map) o);
61              }
62          }
63          
64  
65          // Generate the response
66          String thumbs=generateThumbs(results);
67          
68          response.setContentType("text/html");
69          PrintWriter out = response.getWriter();
70          out.println("<html><head>");
71          out.println(STYLE);
72          out.println("</head><body><small>");
73  
74          long now = System.nanoTime();
75          long total=now-start;
76  
77          out.print("<b>Blocking: "+request.getParameter(ITEMS_PARAM)+"</b><br/>");
78          out.print("Total Time: "+ms(total)+"ms<br/>");
79          out.print("Thread held (<span class='red'>red</span>): "+ms(total)+"ms<br/>");
80          
81          out.println("<img border='0px' src='asyncrest/red.png'   height='20px' width='"+width(total)+"px'>");
82          
83          out.println("<hr />");
84          out.println(thumbs);
85          out.println("</small>");
86          out.println("</body></html>");
87          out.close();
88          
89          
90  
91      }
92  
93      /**
94       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
95       *      response)
96       */
97      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
98      {
99          doGet(request, response);
100     }
101 
102 }