1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
38
39
40
41
42
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 @Override
62 public void init(ServletConfig servletConfig) throws ServletException
63 {
64 if (servletConfig.getInitParameter(APPID_PARAM) == null)
65 _appid = __DEFAULT_APPID;
66 else
67 _appid = servletConfig.getInitParameter(APPID_PARAM);
68 }
69
70
71 public static String sanitize(String s)
72 {
73 if (s==null)
74 return null;
75 return s.replace("<","?").replace("&","?").replace("\n","?");
76 }
77
78 protected String restURL(String item)
79 {
80 try
81 {
82 return ("http://open.api.ebay.com/shopping?MaxEntries=3&appid=" + _appid +
83 "&version=573&siteid=0&callname=FindItems&responseencoding=JSON&QueryKeywords=" +
84 URLEncoder.encode(item,"UTF-8"));
85 }
86 catch(Exception e)
87 {
88 throw new RuntimeException(e);
89 }
90 }
91
92 protected String generateThumbs(Queue<Map<String,String>> results)
93 {
94 StringBuilder thumbs = new StringBuilder();
95 for (Map<String, String> m : results)
96 {
97 if (!m.containsKey("GalleryURL"))
98 continue;
99
100 thumbs.append("<a href=\""+m.get("ViewItemURLForNaturalSearch")+"\">");
101 thumbs.append("<img class='thumb' border='1px' height='25px'"+
102 " src='"+m.get("GalleryURL")+"'"+
103 " title='"+m.get("Title")+"'"+
104 "/>");
105 thumbs.append("</a> ");
106 }
107 return thumbs.toString();
108 }
109
110 protected String ms(long nano)
111 {
112 BigDecimal dec = new BigDecimal(nano);
113 return dec.divide(new BigDecimal(1000000L)).setScale(1,RoundingMode.UP).toString();
114 }
115
116 protected int width(long nano)
117 {
118 int w=(int)((nano+999999L)/5000000L);
119 if (w==0)
120 w=2;
121 return w;
122 }
123
124 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
125 {
126 doGet(request, response);
127 }
128
129 }