Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauthen.servlet;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URISyntaxException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.utils.URIBuilder;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.entity.mime.MultipartEntityBuilder;
- import org.apache.http.impl.client.HttpClientBuilder;
- import com.google.gson.JsonArray;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- public class OneChatServlet {
- // public static void GetList(String friendEmail ,String FileLocation) throws IOException, URISyntaxException {
- public static void doPost(HttpServletRequest request, HttpServletResponse response ,String friendEmail ,String FileLocation)
- throws ServletException, IOException, URISyntaxException {
- JsonObject json = new JsonObject();
- json.addProperty("bot_id", "B4ea02220c51e5d7595f2c31221743452");
- BufferedReader br = null;
- String output;
- StringBuilder responseBuilder = null;
- HttpClient httpClient = HttpClientBuilder.create().build();
- URIBuilder uriBuilder;
- uriBuilder = new URIBuilder("https://chat-manage.one.th:8997/api/v1/getlistroom");
- HttpPost postMethod = new HttpPost(uriBuilder.build());
- StringEntity params = new StringEntity(json.toString());
- postMethod.addHeader("content-type", "application/json");
- postMethod.addHeader("Authorization", "Bearer " + "A294188f4dfca562d81babe073561edfc79cb5150869741b89463b1733c7ad48516646374ba844b038be59d23f9d94fcc");
- postMethod.setEntity(params);
- HttpResponse httpResponse = httpClient.execute(postMethod);
- int responseCode = httpResponse.getStatusLine().getStatusCode();
- if (responseCode == 201 || responseCode == 200) {
- br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));
- responseBuilder = new StringBuilder();
- while ((output = br.readLine()) != null) {
- responseBuilder.append(output);
- }
- System.out.println(httpResponse.getStatusLine().getStatusCode());
- } else {
- System.out.println("Failed : HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
- }
- String user_id = FindUser(responseBuilder.toString(),friendEmail);
- if(user_id == null)
- LogoutServlet.doLogout(request, response, "user_id == null");
- SendFile(FileLocation, user_id);
- }
- public static String FindUser(String response,String email){
- String GetList = response;
- JsonObject jsonObject = new JsonParser().parse(GetList ).getAsJsonObject();
- System.out.println(GetList );
- JsonArray data = jsonObject.getAsJsonArray("list_friend");
- for (int i = 0; i < data.size(); ++i) {
- JsonObject dataObj = (JsonObject) data.get(i);
- if(dataObj.get("one_email").getAsString().equals(email)){
- System.out.println(dataObj.get("user_id").getAsString());
- return dataObj.get("user_id").getAsString();
- }
- else {
- System.out.println("can not find");
- }
- }
- return null;
- }
- public static void SendFile(String FileLocation, String user_id) throws ClientProtocolException, IOException, URISyntaxException {
- BufferedReader br = null;
- String output;
- StringBuilder responseBuilder = null;
- HttpClient httpClient = HttpClientBuilder.create().build();
- URIBuilder uriBuilder;
- uriBuilder = new URIBuilder("https://chat-public.one.th:8034/api/v1/push_message");
- HttpPost postMethod = new HttpPost(uriBuilder.build());
- HttpEntity params = MultipartEntityBuilder.create()
- .addTextBody("to", user_id)
- .addTextBody("bot_id", "B4ea02220c51e5d7595f2c31221743452")
- .addTextBody("type", "file")
- .addBinaryBody("file", new File(FileLocation)).build();
- // postMethod.addHeader("content-type", "application/json");
- postMethod.addHeader("Authorization", "Bearer " + "A294188f4dfca562d81babe073561edfc79cb5150869741b89463b1733c7ad48516646374ba844b038be59d23f9d94fcc");
- postMethod.setEntity(params);
- HttpResponse httpResponse = httpClient.execute(postMethod);
- int responseCode = httpResponse.getStatusLine().getStatusCode();
- if (responseCode == 201 || responseCode == 200) {
- br = new BufferedReader(new InputStreamReader((httpResponse.getEntity().getContent())));
- responseBuilder = new StringBuilder();
- while ((output = br.readLine()) != null) {
- responseBuilder.append(output);
- }
- System.out.println(httpResponse.getStatusLine().getStatusCode());
- } else {
- System.out.println("Failed : HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement