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.IOException;
18  import java.math.BigDecimal;
19  import java.math.RoundingMode;
20  import java.net.URLEncoder;
21  import java.util.Map;
22  import java.util.Queue;
23  
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  /**
31   * Abstract Servlet implementation class AsyncRESTServlet.
32   * Enquires ebay REST service for auctions by key word.
33   * May be configured with init parameters: <dl>
34   * <dt>appid</dt><dd>The eBay application ID to use</dd>
35   * </dl>
36   * Each request examines the following request parameters:<dl>
37   * <dt>items</dt><dd>The keyword to search for</dd>
38   * </dl>
39   */
40  public class AbstractRestServlet extends HttpServlet
41  {
42      protected final static String __DEFAULT_APPID = "Webtide81-adf4-4f0a-ad58-d91e41bbe85";
43      protected final static String STYLE = 
44          "<style type='text/css'>"+
45          "  img.thumb:hover {height:50px}"+
46          "  img.thumb {vertical-align:text-top}"+
47          "  span.red {color: #ff0000}"+
48          "  span.green {color: #00ff00}"+
49          "  iframe {border: 0px}"+
50          "</style>";
51  
52      protected final static String ITEMS_PARAM = "items";
53      protected final static String APPID_PARAM = "appid";
54  
55      protected String _appid;
56  
57      public void init(ServletConfig servletConfig) throws ServletException
58      {
59          if (servletConfig.getInitParameter(APPID_PARAM) == null)
60              _appid = __DEFAULT_APPID;
61          else
62              _appid = servletConfig.getInitParameter(APPID_PARAM);
63      }
64      
65      protected String restURL(String item) 
66      {
67          try
68          {
69              return ("http://open.api.ebay.com/shopping?MaxEntries=3&appid=" + _appid + 
70                      "&version=573&siteid=0&callname=FindItems&responseencoding=JSON&QueryKeywords=" + 
71                      URLEncoder.encode(item,"UTF-8"));
72          }
73          catch(Exception e)
74          {
75              throw new RuntimeException(e);
76          }
77      }
78      
79      protected String generateThumbs(Queue<Map<String,String>> results)
80      {
81          StringBuilder thumbs = new StringBuilder();
82          for (Map<String, String> m : results)
83          {
84              if (!m.containsKey("GalleryURL"))
85                  continue;
86                  
87              thumbs.append("<a href=\""+m.get("ViewItemURLForNaturalSearch")+"\">");
88              thumbs.append("<img class='thumb' border='1px' height='25px'"+
89                          " src='"+m.get("GalleryURL")+"'"+
90                          " title='"+m.get("Title")+"'"+
91                          "/>");
92              thumbs.append("</a>&nbsp;");
93          }
94          return thumbs.toString();
95      }
96  
97      protected String ms(long nano)
98      {
99          BigDecimal dec = new BigDecimal(nano);
100         return dec.divide(new BigDecimal(1000000L)).setScale(1,RoundingMode.UP).toString();
101     }
102     
103     protected int width(long nano)
104     {
105         int w=(int)((nano+999999L)/5000000L);
106         if (w==0)
107             w=2;
108         return w;
109     }
110     
111     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
112     {
113         doGet(request, response);
114     }
115 
116 }