1
2
3
4
5
6
7
8
9
10
11
12
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
32
33
34
35
36
37
38
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> ");
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 }