Skip to content

Instantly share code, notes, and snippets.

@keesun
Last active February 17, 2021 10:09

Revisions

  1. Keesun Baik (a.k.a, Whiteship) revised this gist Jan 24, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions ApiClientUtils.java
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,10 @@ public class ApiClientUtils {
    RestTemplate restTemplate = new RestTemplate();
    ObjectMapper objectMapper = new ObjectMapper();

    private String getApiServerUrl(String domain, int apiPort) {
    return "http://" + domain + ":" + apiPort;
    }

    /**
    * 보낼 메시지 크기가 큰 경우, 혹은 한글이 포함된 경우 POST로 보내는 것이 안전합니다.
    *
    @@ -115,8 +119,4 @@ public String getServerUrlByGet(String domain, int apiPort, String appId, String
    return jsonMap != null ? (String) jsonMap.get("url") : null;
    }

    private String getApiServerUrl(String domain, int apiPort) {
    return "http://" + domain + ":" + apiPort;
    }

    }
  2. Keesun Baik (a.k.a, Whiteship) revised this gist Jan 23, 2013. 1 changed file with 84 additions and 23 deletions.
    107 changes: 84 additions & 23 deletions ApiClientUtils.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,17 @@
    package herma.common;

    import org.codehaus.jackson.map.ObjectMapper;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.web.client.RestTemplate;

    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Map;

    /**
    *
    * API Server 사용시 참고용 코드
    @@ -6,35 +20,62 @@
    */
    public class ApiClientUtils {

    RestTemplate restTemplate = new RestTemplate();
    ObjectMapperUtils objectMapperUtils = new ObjectMapperUtils();
    RestTemplate restTemplate = new RestTemplate();
    ObjectMapper objectMapper = new ObjectMapper();

    /**
    * 보낼 메시지 크기가 큰 경우, 혹은 한글이 포함된 경우 POST로 보내는 것이 안전합니다.
    *
    * @param domain 애플리케이션에 할당된 도메인
    * @param apiPort API Port 번호
    * @param appId 애플리케이션 아이디
    * @param authKey 애플리케이션의 Auth Key
    * @param channelName 이벤트와 메시지를 보낼 채널
    * @param event 이벤트
    * @param message 메시지
    */
    public void sendByPost(String domain, int apiPort, String appId, String authKey, String channelName, String event, String message) {
    String serverUrl = "http://" + domain + ":" + apiPort;
    String serverUrl = getApiServerUrl(domain, apiPort);

    Map<String, Object> params = new HashMap<String, Object>();
    Map<String, Object> params = new HashMap<>();
    params.put("channels", Arrays.asList(channelName));
    params.put("event", event);
    params.put("data", message);
    String body = objectMapperUtils.write(params);

    //set your headers
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
    String body = null;
    try {
    body = objectMapper.writeValueAsString(params);
    } catch (IOException e) {
    throw new RuntimeException(e);
    }

    //set your entity to send
    HttpEntity entity = new HttpEntity(body, headers);
    if(body != null) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));

    // send it!
    restTemplate.postForEntity(
    serverUrl + "/channel/{app}?auth_key={auth_key}",
    entity,
    String.class,
    appId, authKey);
    HttpEntity entity = new HttpEntity(body, headers);

    restTemplate.postForEntity(
    serverUrl + "/channel/{app}?auth_key={auth_key}",
    entity,
    String.class,
    appId, authKey);
    }
    }

    /**
    * 메시지가 짧거나 한글 메시지가 아닌 경우라면 GET으로도 보낼 수 있습니다.
    *
    * @param domain 애플리케이션에 할당된 도메인
    * @param apiPort API Port 번호
    * @param appId 애플리케이션 아이디
    * @param authKey 애플리케이션의 Auth Key
    * @param channelName 이벤트와 메시지를 보낼 채널
    * @param event 이벤트
    * @param message 메시지
    */
    public void sendByGet(String domain, int apiPort, String appId, String authKey, String channelName, String event, String message) {
    String serverUrl = "http://" + domain + ":" + apiPort;
    String serverUrl = getApiServerUrl(domain, apiPort);

    Map<String, Object> params = new HashMap<>();
    params.put("channels", channelName);
    @@ -49,13 +90,33 @@ public void sendByGet(String domain, int apiPort, String appId, String authKey,
    params);
    }

    public String accessByGet(String domain, int apiPort, String appId, String authKey){
    String accessUrl = "http://" + domain + ":" + apiPort;
    /**
    * 접속할 Push Server URL을 받아옵니다.
    *
    * @param domain 애플리케이션에 할당된 도메인
    * @param apiPort API Port 번호
    * @param appId 애플리케이션 아이디
    * @param authKey 애플리케이션의 Auth Key 또는 Access Key
    * @return Push Server URL
    */
    public String getServerUrlByGet(String domain, int apiPort, String appId, String authKey){
    String accessUrl = getApiServerUrl(domain, apiPort);
    accessUrl += "/access/" + appId + "?auth_key=" + authKey;

    String result = restTemplate.getForObject(accessUrl, String.class, null);
    Map<String, Object> jsonMap = objectMapperUtils.read(result, Map.class);
    String serverUrl = (String) jsonMap.get("url");
    return serverUrl;

    Map<String, Object> jsonMap = null;
    try {
    jsonMap = objectMapper.readValue(result, Map.class);
    } catch (IOException e) {
    throw new RuntimeException(e);
    }

    return jsonMap != null ? (String) jsonMap.get("url") : null;
    }
    }

    private String getApiServerUrl(String domain, int apiPort) {
    return "http://" + domain + ":" + apiPort;
    }

    }
  3. Keesun Baik (a.k.a, Whiteship) created this gist Jan 23, 2013.
    61 changes: 61 additions & 0 deletions ApiClientUtils.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    /**
    *
    * API Server 사용시 참고용 코드
    *
    * @author Keesun Baik
    */
    public class ApiClientUtils {

    RestTemplate restTemplate = new RestTemplate();
    ObjectMapperUtils objectMapperUtils = new ObjectMapperUtils();

    public void sendByPost(String domain, int apiPort, String appId, String authKey, String channelName, String event, String message) {
    String serverUrl = "http://" + domain + ":" + apiPort;

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("channels", Arrays.asList(channelName));
    params.put("event", event);
    params.put("data", message);
    String body = objectMapperUtils.write(params);

    //set your headers
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));

    //set your entity to send
    HttpEntity entity = new HttpEntity(body, headers);

    // send it!
    restTemplate.postForEntity(
    serverUrl + "/channel/{app}?auth_key={auth_key}",
    entity,
    String.class,
    appId, authKey);
    }

    public void sendByGet(String domain, int apiPort, String appId, String authKey, String channelName, String event, String message) {
    String serverUrl = "http://" + domain + ":" + apiPort;

    Map<String, Object> params = new HashMap<>();
    params.put("channels", channelName);
    params.put("event", event);
    params.put("auth_key", authKey);
    params.put("data", message);
    params.put("app_id", appId);

    restTemplate.getForObject(
    serverUrl + "/channel/{app_id}?channels={channels}&event={event}&auth_key={auth_key}&data={data}",
    String.class,
    params);
    }

    public String accessByGet(String domain, int apiPort, String appId, String authKey){
    String accessUrl = "http://" + domain + ":" + apiPort;
    accessUrl += "/access/" + appId + "?auth_key=" + authKey;

    String result = restTemplate.getForObject(accessUrl, String.class, null);
    Map<String, Object> jsonMap = objectMapperUtils.read(result, Map.class);
    String serverUrl = (String) jsonMap.get("url");
    return serverUrl;
    }
    }