1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.maps.service;
17
18 import java.io.BufferedReader;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21
22 import javax.ws.rs.GET;
23 import javax.ws.rs.Path;
24 import javax.ws.rs.QueryParam;
25
26 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
27 import org.apache.commons.httpclient.HttpClient;
28 import org.apache.commons.httpclient.methods.GetMethod;
29 import org.apache.commons.httpclient.params.HttpClientParams;
30 import org.apache.commons.httpclient.params.HttpConnectionParams;
31 import org.apache.commons.httpclient.params.HttpMethodParams;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.kuali.mobility.configparams.service.ConfigParamService;
35 import org.kuali.mobility.maps.entity.Location;
36 import org.kuali.mobility.maps.entity.MapsGroup;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Qualifier;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.stereotype.Service;
42
43
44
45
46
47
48
49 @Service
50 public class CXFMapsService {
51
52
53 private static final Logger LOG = LoggerFactory.getLogger(CXFMapsService.class);
54 private static final String FOURSQUARE_CLIENT_ID = "Maps.Foursquare.Client.Id";
55 private static final String FOURSQUARE_CLIENT_SECRET = "Maps.Foursquare.Client.Secret";
56
57 @Autowired
58 @Qualifier("mapsService")
59 private MapsService service;
60
61 @Autowired
62 private ConfigParamService configParamService;
63
64 @GET
65 @Path("/group/{groupCode}")
66 public Object getBuildings(@QueryParam("groupCode") String groupId) {
67 try {
68 MapsGroup group = this.getService().getMapsGroupById(groupId);
69 if (group != null) {
70 return group.toJson();
71 }
72 } catch (Exception e) {
73 LOG.error(e.getMessage(), e);
74 }
75 return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
76 }
77
78 @GET
79 @Path(value = "/building/{id}")
80 public Object get(@QueryParam("id") String id) {
81 Location location = this.getService().getLocationById(id);
82 if (location != null) {
83 return location.toJson();
84 }
85 return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
86 }
87
88 @GET
89 @Path(value = "/foursquare")
90 public String getFoursquareData(@QueryParam("lat") String latitude, @QueryParam("lng") String longitude) throws Exception {
91 String foursquareId = "";
92 String foursquareSecret = "";
93 try {
94 foursquareId = getConfigParamService().findValueByName(FOURSQUARE_CLIENT_ID);
95 foursquareSecret = getConfigParamService().findValueByName(FOURSQUARE_CLIENT_SECRET);
96 } catch (Exception e) {
97 LOG.error("Foursquare config parameters are not set.");
98 }
99
100 BufferedReader br = null;
101 GetMethod get = null;
102 StringBuilder sb = new StringBuilder();
103 try {
104 get = new GetMethod("https://api.foursquare.com/v2/venues/search?v=20110627&ll="+ latitude + "," + longitude +"&limit=8&client_id=" + foursquareId + "&client_secret=" + foursquareSecret);
105 br = new BufferedReader(new InputStreamReader(getInputStreamFromGetMethod(get, 10000)));
106 String line = null;
107 while ((line = br.readLine()) != null) {
108 sb.append(line);
109 }
110 } catch (Exception e) {
111
112 } finally {
113 if (br != null) {
114 br.close();
115 }
116 if (get != null) {
117 get.releaseConnection();
118 }
119 }
120
121 return sb.toString();
122 }
123
124 @GET
125 @Path(value = "/foursquare/{id}")
126 public String getFoursquareData(@QueryParam("id") String id) throws Exception {
127 String foursquareId = "";
128 String foursquareSecret = "";
129 try {
130 foursquareId = getConfigParamService().findValueByName(FOURSQUARE_CLIENT_ID);
131 foursquareSecret = getConfigParamService().findValueByName(FOURSQUARE_CLIENT_SECRET);
132 } catch (Exception e) {
133 LOG.error("Foursquare config parameters are not set.");
134 }
135
136 BufferedReader br = null;
137 GetMethod get = null;
138 StringBuilder sb = new StringBuilder();
139 try {
140 get = new GetMethod("https://api.foursquare.com/v2/venues/" + id + "?client_id=" + foursquareId + "&client_secret=" + foursquareSecret);
141 br = new BufferedReader(new InputStreamReader(getInputStreamFromGetMethod(get, 10000)));
142 String line = null;
143 while ((line = br.readLine()) != null) {
144 sb.append(line);
145 }
146 } catch (Exception e) {
147
148 } finally {
149 if (br != null) {
150 br.close();
151 }
152 if (get != null) {
153 get.releaseConnection();
154 }
155 }
156
157 return sb.toString();
158 }
159
160 private InputStream getInputStreamFromGetMethod(GetMethod get, int timeout) throws Exception {
161 get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
162 HttpClient httpClient = new HttpClient();
163 httpClient.getParams().setParameter(HttpClientParams.SO_TIMEOUT, new Integer(timeout));
164 httpClient.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(timeout));
165 httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, new Integer(timeout));
166 int status = httpClient.executeMethod(get);
167 if (status == HttpStatus.OK.value()) {
168 return get.getResponseBodyAsStream();
169 }
170 return null;
171 }
172
173 public MapsService getService() {
174 return service;
175 }
176 public void setService(MapsService service) {
177 this.service = service;
178 }
179
180 public ConfigParamService getConfigParamService() {
181 return configParamService;
182 }
183
184 public void setConfigParamService(ConfigParamService configParamService) {
185 this.configParamService = configParamService;
186 }
187 }