View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.example.asyncrest;
20  
21  import java.io.IOException;
22  import java.math.BigDecimal;
23  import java.math.RoundingMode;
24  import java.net.URLEncoder;
25  import java.util.Map;
26  import java.util.Queue;
27  
28  import javax.servlet.ServletConfig;
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServlet;
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  
34  /**
35   * Abstract Servlet implementation class AsyncRESTServlet.
36   * Enquires ebay REST service for auctions by key word.
37   * May be configured with init parameters: <dl>
38   * <dt>appid</dt><dd>The eBay application ID to use</dd>
39   * </dl>
40   * Each request examines the following request parameters:<dl>
41   * <dt>items</dt><dd>The keyword to search for</dd>
42   * </dl>
43   */
44  public class AbstractRestServlet extends HttpServlet
45  {
46      protected final static String __DEFAULT_APPID = "Webtide81-adf4-4f0a-ad58-d91e41bbe85";
47      protected final static String STYLE = 
48          "<style type='text/css'>"+
49          "  img.thumb:hover {height:50px}"+
50          "  img.thumb {vertical-align:text-top}"+
51          "  span.red {color: #ff0000}"+
52          "  span.green {color: #00ff00}"+
53          "  iframe {border: 0px}"+
54          "</style>";
55  
56      protected final static String ITEMS_PARAM = "items";
57      protected final static String APPID_PARAM = "appid";
58  
59      protected String _appid;
60  
61      public void init(ServletConfig servletConfig) throws ServletException
62      {
63          if (servletConfig.getInitParameter(APPID_PARAM) == null)
64              _appid = __DEFAULT_APPID;
65          else
66              _appid = servletConfig.getInitParameter(APPID_PARAM);
67      }
68      
69      protected String restURL(String item) 
70      {
71          try
72          {
73              return ("http://open.api.ebay.com/shopping?MaxEntries=3&appid=" + _appid + 
74                      "&version=573&siteid=0&callname=FindItems&responseencoding=JSON&QueryKeywords=" + 
75                      URLEncoder.encode(item,"UTF-8"));
76          }
77          catch(Exception e)
78          {
79              throw new RuntimeException(e);
80          }
81      }
82      
83      protected String generateThumbs(Queue<Map<String,String>> results)
84      {
85          StringBuilder thumbs = new StringBuilder();
86          for (Map<String, String> m : results)
87          {
88              if (!m.containsKey("GalleryURL"))
89                  continue;
90                  
91              thumbs.append("<a href=\""+m.get("ViewItemURLForNaturalSearch")+"\">");
92              thumbs.append("<img class='thumb' border='1px' height='25px'"+
93                          " src='"+m.get("GalleryURL")+"'"+
94                          " title='"+m.get("Title")+"'"+
95                          "/>");
96              thumbs.append("</a>&nbsp;");
97          }
98          return thumbs.toString();
99      }
100 
101     protected String ms(long nano)
102     {
103         BigDecimal dec = new BigDecimal(nano);
104         return dec.divide(new BigDecimal(1000000L)).setScale(1,RoundingMode.UP).toString();
105     }
106     
107     protected int width(long nano)
108     {
109         int w=(int)((nano+999999L)/5000000L);
110         if (w==0)
111             w=2;
112         return w;
113     }
114     
115     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
116     {
117         doGet(request, response);
118     }
119 
120 }