[#123] added some APIs to testapp

This commit is contained in:
Hyun Jeong
2014-12-17 19:02:35 +09:00
parent d2b1bc5c1f
commit 3d70edff36
8 changed files with 152 additions and 26 deletions
+7
View File
@@ -117,6 +117,13 @@
<artifactId>httpcore</artifactId>
<version>4.3.3</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.4</version>
</dependency>
</dependencies>
<build>
@@ -1,44 +1,39 @@
package com.navercorp.pinpoint.testapp.controller;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.navercorp.pinpoint.testapp.service.remote.RemoteService;
import com.navercorp.pinpoint.testapp.util.Description;
@Controller
@RequestMapping("/httpclient4")
public class HttpClient4Controller {
@RequestMapping("/queryGoogle")
private static final String DEFAULT_GET_GEOCODE_ADDRESS = "Gyeonggi-do, Seongnam-si, Bundang-gu, Jeongja-dong, 178-1";
private static final String DEFAULT_GET_TWITTER_URL_COUNT_URL = "http://www.naver.com";
@Autowired
@Qualifier("closeableHttpClientRemoteService")
RemoteService remoteService;
@RequestMapping("/getGeoCode")
@Description("")
@ResponseBody
public String queryGoogle() throws Exception {
CloseableHttpClient httpClient = HttpClients.createMinimal();
HttpGet httpGet = new HttpGet(buildQueryUri("www.google.com", "pinpoint"));
httpClient.execute(httpGet);
return "queryGoogle";
public Map<String, Object> getGeoCode(@RequestParam(defaultValue = DEFAULT_GET_GEOCODE_ADDRESS, required = false) String address) throws Exception {
return remoteService.getGeoCode(address);
}
@RequestMapping("/queryNaver")
@RequestMapping("/getTwitterUrlCount")
@ResponseBody
public String queryNaver() throws Exception {
CloseableHttpClient httpClient = HttpClients.createMinimal();
HttpGet httpGet = new HttpGet(buildQueryUri("www.naver.com", "pinpoint"));
httpClient.execute(httpGet);
return "queryNaver";
public Map<String, Object> getTwitterUrlCount(@RequestParam(defaultValue = DEFAULT_GET_TWITTER_URL_COUNT_URL, required = false) String url) throws Exception {
return remoteService.getTwitterUrlCount(url);
}
private URI buildQueryUri(String host, String queryParam) throws URISyntaxException {
return new URIBuilder()
.setScheme("http")
.setHost(host)
.setPath("/search")
.setParameter("q", queryParam)
.build();
}
}
@@ -0,0 +1,18 @@
package com.navercorp.pinpoint.testapp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.navercorp.pinpoint.testapp.util.Description;
@Controller
public class SimpleController {
@RequestMapping("/getCurrentTimestamp")
@Description("Returns the current timestamp of the server (in ms).")
@ResponseBody
public Long getCurrentTimestamp() {
return System.currentTimeMillis();
}
}
@@ -0,0 +1,55 @@
package com.navercorp.pinpoint.testapp.service.remote;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Map;
import org.apache.commons.codec.CharEncoding;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.web.client.RestClientException;
public abstract class AbstractRemoteService implements RemoteService {
@Override
public final Map<String, Object> getGeoCode(String address) {
try {
String encodedAddress = URLEncoder.encode(address, CharEncoding.UTF_8);
URI uri = new URIBuilder()
.setScheme("http")
.setHost("maps.googleapis.com")
.setPath("/maps/api/geocode/json")
.setParameter("address", encodedAddress)
.setParameter("sensor", "false")
.build();
@SuppressWarnings("unchecked")
Map<String, Object> response = this.getForObject(uri, Map.class);
return response;
} catch (UnsupportedEncodingException e) {
throw new RestClientException(e.getMessage());
} catch (URISyntaxException e) {
throw new RestClientException(e.getMessage());
}
}
@Override
public Map<String, Object> getTwitterUrlCount(String url) {
try {
URI uri = new URIBuilder()
.setScheme("http")
.setHost("urls.api.twitter.com")
.setPath("/1/urls/count.json")
.setParameter("url", url)
.build();
@SuppressWarnings("unchecked")
Map<String, Object> response = this.getForObject(uri, Map.class);
return response;
} catch (URISyntaxException e) {
throw new RestClientException(e.getMessage());
}
}
protected abstract <T> T getForObject(URI url, Class<T> responseType);
}
@@ -0,0 +1,22 @@
package com.navercorp.pinpoint.testapp.service.remote;
import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public final class CloseableHttpClientRemoteService extends AbstractRemoteService {
@Autowired
@Qualifier("closeableHttpClientRestTemplate")
private RestTemplate restTemplate;
@Override
protected <T> T getForObject(URI url, Class<T> responseType) {
return this.restTemplate.getForObject(url, responseType);
}
}
@@ -0,0 +1,10 @@
package com.navercorp.pinpoint.testapp.service.remote;
import java.util.Map;
public interface RemoteService {
public Map<String, Object> getGeoCode(String address);
public Map<String, Object> getTwitterUrlCount(String url);
}
@@ -0,0 +1,12 @@
package com.navercorp.pinpoint.testapp.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Description {
String value();
}
@@ -10,6 +10,13 @@
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven/>
<context:component-scan base-package="com.navercorp.pinpoint.testapp.controller" />
<context:component-scan base-package="com.navercorp.pinpoint.testapp.service" />
<bean id="httpComponentsClientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory" />
<bean id="closeableHttpClientRestTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="httpComponentsClientHttpRequestFactory" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">