Advertisement
ballchaichana

onechat

Oct 18th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.03 KB | None | 0 0
  1. package th.in.oneauthen.servlet;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.URISyntaxException;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12.  
  13. import org.apache.http.HttpEntity;
  14. import org.apache.http.HttpResponse;
  15. import org.apache.http.client.ClientProtocolException;
  16. import org.apache.http.client.HttpClient;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.client.utils.URIBuilder;
  19. import org.apache.http.entity.StringEntity;
  20. import org.apache.http.entity.mime.MultipartEntityBuilder;
  21. import org.apache.http.impl.client.HttpClientBuilder;
  22.  
  23. import com.google.gson.JsonArray;
  24. import com.google.gson.JsonObject;
  25. import com.google.gson.JsonParser;
  26.  
  27. public class OneChatServlet {
  28.    
  29. //  public static void GetList(String friendEmail ,String FileLocation) throws IOException, URISyntaxException {
  30.            public static void doPost(HttpServletRequest request, HttpServletResponse response ,String friendEmail ,String FileLocation)
  31.                    throws ServletException, IOException, URISyntaxException {
  32.         JsonObject json = new JsonObject();
  33.         json.addProperty("bot_id", "B4ea02220c51e5d7595f2c31221743452");
  34.  
  35.  
  36.         BufferedReader br = null;
  37.         String output;
  38.         StringBuilder responseBuilder = null;
  39.  
  40.         HttpClient httpClient = HttpClientBuilder.create().build();
  41.         URIBuilder uriBuilder;
  42.  
  43.  
  44.             uriBuilder = new URIBuilder("https://chat-manage.one.th:8997/api/v1/getlistroom");
  45.             HttpPost postMethod = new HttpPost(uriBuilder.build());
  46.             StringEntity params = new StringEntity(json.toString());
  47.  
  48.             postMethod.addHeader("content-type", "application/json");
  49.             postMethod.addHeader("Authorization", "Bearer " + "A294188f4dfca562d81babe073561edfc79cb5150869741b89463b1733c7ad48516646374ba844b038be59d23f9d94fcc");
  50.             postMethod.setEntity(params);
  51.  
  52.             HttpResponse httpResponse = httpClient.execute(postMethod);
  53.             int responseCode = httpResponse.getStatusLine().getStatusCode();
  54.             if (responseCode == 201 || responseCode == 200) {
  55.                 br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));
  56.                 responseBuilder = new StringBuilder();
  57.                 while ((output = br.readLine()) != null) {
  58.                     responseBuilder.append(output);
  59.                 }
  60.                 System.out.println(httpResponse.getStatusLine().getStatusCode());
  61.  
  62.             } else {
  63.                 System.out.println("Failed : HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
  64.             }
  65.              
  66.             String user_id =  FindUser(responseBuilder.toString(),friendEmail);
  67.             if(user_id == null)
  68.                 LogoutServlet.doLogout(request, response, "user_id == null");
  69.                  
  70.             SendFile(FileLocation, user_id);
  71.            
  72.              
  73.    
  74.              
  75.  
  76.  
  77.     }
  78.  
  79.     public static String FindUser(String response,String email){
  80.  
  81.         String GetList = response;
  82.         JsonObject jsonObject = new JsonParser().parse(GetList ).getAsJsonObject();
  83.  
  84.        
  85.         System.out.println(GetList );
  86.  
  87.         JsonArray data = jsonObject.getAsJsonArray("list_friend");
  88.         for (int i = 0; i < data.size(); ++i) {
  89.             JsonObject dataObj = (JsonObject) data.get(i);
  90.  
  91.            
  92.            
  93.             if(dataObj.get("one_email").getAsString().equals(email)){
  94.                    System.out.println(dataObj.get("user_id").getAsString());
  95.                    return dataObj.get("user_id").getAsString();
  96.                 }
  97.             else {
  98.                 System.out.println("can not find");
  99.             }
  100.                
  101.  
  102.         }
  103.  
  104.  
  105.         return null;
  106.  
  107.     }
  108.    
  109.     public static void SendFile(String FileLocation, String user_id) throws ClientProtocolException, IOException, URISyntaxException {
  110.        
  111.  
  112.        
  113.  
  114.         BufferedReader br = null;
  115.         String output;
  116.         StringBuilder responseBuilder = null;
  117.  
  118.         HttpClient httpClient = HttpClientBuilder.create().build();
  119.         URIBuilder uriBuilder;
  120.  
  121.  
  122.             uriBuilder = new URIBuilder("https://chat-public.one.th:8034/api/v1/push_message");
  123.             HttpPost postMethod = new HttpPost(uriBuilder.build());
  124.  
  125.            
  126.             HttpEntity params = MultipartEntityBuilder.create()
  127.                     .addTextBody("to", user_id)
  128.                     .addTextBody("bot_id", "B4ea02220c51e5d7595f2c31221743452")
  129.                     .addTextBody("type", "file")
  130.                     .addBinaryBody("file", new File(FileLocation)).build();
  131.  
  132. //          postMethod.addHeader("content-type", "application/json");
  133.             postMethod.addHeader("Authorization", "Bearer " + "A294188f4dfca562d81babe073561edfc79cb5150869741b89463b1733c7ad48516646374ba844b038be59d23f9d94fcc");
  134.             postMethod.setEntity(params);
  135.  
  136.             HttpResponse httpResponse = httpClient.execute(postMethod);
  137.             int responseCode = httpResponse.getStatusLine().getStatusCode();
  138.             if (responseCode == 201 || responseCode == 200) {
  139.                 br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));
  140.                 responseBuilder = new StringBuilder();
  141.                 while ((output = br.readLine()) != null) {
  142.                     responseBuilder.append(output);
  143.                 }
  144.                 System.out.println(httpResponse.getStatusLine().getStatusCode());
  145.  
  146.             } else {
  147.                 System.out.println("Failed : HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
  148.             }
  149.              
  150.  
  151.        
  152.     }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement