From 0f305fb3278de30504446f83c8fbb57cccd8a093 Mon Sep 17 00:00:00 2001 From: Ahab Hyde Date: Fri, 20 Apr 2018 15:29:44 +0200 Subject: [PATCH] Aggiunta richiesta post di upload file funzionante --- .../em/miguelbridge/matrixbot/MatrixBot.java | 49 +++++++++++++++++-- .../matrixbot/RequestHandler.java | 44 +++++++---------- src/test/UploadTest.java | 23 ++++----- 3 files changed, 73 insertions(+), 43 deletions(-) diff --git a/src/com/em/miguelbridge/matrixbot/MatrixBot.java b/src/com/em/miguelbridge/matrixbot/MatrixBot.java index fdd3b3c..e86a1e2 100644 --- a/src/com/em/miguelbridge/matrixbot/MatrixBot.java +++ b/src/com/em/miguelbridge/matrixbot/MatrixBot.java @@ -76,12 +76,19 @@ public class MatrixBot { */ public String login() throws IOException, ParseException, URISyntaxException { String requestUrl = homeUrl + "client/r0/login"; - + + /* String[][] reqParams = new String[][] { {"type", "m.login.password"}, {"user", readBotUserName()}, {"password", readPswd()} }; + */ + + JSONObject reqParams = new JSONObject(); + reqParams.put("type", "m.login.password"); + reqParams.put("user", readBotUserName()); + reqParams.put("password", readPswd()); String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams); @@ -94,7 +101,7 @@ public class MatrixBot { String requestUrl = homeUrl + String.format("client/r0/rooms/%s/join?access_token=%s", roomAddress, accessToken); - String[][] reqParams = null; + JSONObject reqParams = new JSONObject(); String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams); return risposta[0]; @@ -103,21 +110,55 @@ public class MatrixBot { public synchronized String sendMessage(String message, String roomAddress) throws IOException, URISyntaxException { String requestUrl = homeUrl + String.format("client/r0/rooms/%s/send/m.room.message?access_token=%s", roomAddress, accessToken); - + + /* String[][] reqParams = new String[][] { {"msgtype", "m.text"}, {"body", message} }; + */ + JSONObject reqParams = new JSONObject(); + reqParams.put("msgtype", "m.text"); + reqParams.put("body", message); String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams); return risposta[0] + " - " + risposta[1]; } - public synchronized String sendFile(String roomAddress, File file) throws IOException { + public synchronized String sendFile(String roomAddress, File file) throws IOException, URISyntaxException, ParseException { String requestUrl = homeUrl + String.format("media/r0/upload?filename=%s&access_token=%s", file.getName(), accessToken); String[] risposta = RequestHandler.postRequestFile(requestUrl, file); + + JSONObject uriFileObj = (JSONObject) new JSONParser().parse(risposta[1]); + String uriFile = (String) uriFileObj.get("content_uri"); + + System.out.println("Il file รจ " + uriFile); + requestUrl = homeUrl + String.format("client/r0/rooms/%s/send/m.room.message?access_token=%s", + roomAddress, accessToken); + + /* + String[][] reqParams = new String[][] { + {"msgtype", "m.image"}, + {"body", "image.png"}, + {"url", uriFile} + }; + */ + + JSONObject reqParams = new JSONObject(); + JSONObject objInfo = new JSONObject(); + + objInfo.put("mimetype", "image/png"); + objInfo.put("size", file.length()); + + reqParams.put("info", objInfo); + reqParams.put("msgtype", "m.file"); + reqParams.put("body", "image.png"); + reqParams.put("url", uriFile); + + risposta = RequestHandler.postRequestJSON(requestUrl, reqParams); + return risposta[0] + " - " + risposta[1]; } diff --git a/src/com/em/miguelbridge/matrixbot/RequestHandler.java b/src/com/em/miguelbridge/matrixbot/RequestHandler.java index 8d7ce41..810db5b 100644 --- a/src/com/em/miguelbridge/matrixbot/RequestHandler.java +++ b/src/com/em/miguelbridge/matrixbot/RequestHandler.java @@ -1,9 +1,6 @@ package com.em.miguelbridge.matrixbot; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; @@ -13,13 +10,18 @@ import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.methods.RequestBuilder; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; +import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; import org.json.simple.JSONObject; /** @@ -48,24 +50,16 @@ public class RequestHandler { return risposta; } - public static String[] postRequestJSON(String inUrl, String[][] reqParams) throws IOException, URISyntaxException { + public static String[] postRequestJSON(String inUrl, JSONObject inObj) throws IOException, URISyntaxException { HttpClient client = HttpClientBuilder.create().build(); HttpPost post = new HttpPost(inUrl); //add header post.setHeader("User-Agent", "Mozilla/5.0"); post.addHeader("Content-Type", "charset=UTF_8"); - - JSONObject obj = new JSONObject(); - if (reqParams != null) { - for (String[] param : reqParams) - obj.put(param[0], param[1]); - } - String jsonString = obj.toJSONString(); - - - StringEntity requestEntity = new StringEntity(obj.toJSONString(), ContentType.APPLICATION_JSON); + String jsonString = inObj.toJSONString(); + StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON); post.setEntity(requestEntity); HttpResponse response = client.execute(post); @@ -84,19 +78,17 @@ public class RequestHandler { } public static String[] postRequestFile(String inUrl, File file) throws IOException { - FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); + CloseableHttpClient httpClient = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(inUrl); + httpPost.setHeader("Content-Type", "application/file"); - MultipartEntityBuilder builder = MultipartEntityBuilder.create(); - builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); - builder.addPart("content", fileBody); - HttpEntity entity = builder.build(); - - HttpPost request = new HttpPost(inUrl); - request.setEntity(entity); - - HttpClient client = HttpClientBuilder.create().build(); - HttpResponse response = client.execute(request); + byte[] b = new byte[(int) file.length()]; + FileInputStream fileInputStream = new FileInputStream(file); + fileInputStream.read(b); + HttpEntity entity = new ByteArrayEntity(b); + httpPost.setEntity(entity); + HttpResponse response = httpClient.execute(httpPost); BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); diff --git a/src/test/UploadTest.java b/src/test/UploadTest.java index 098b5ef..a94ce04 100644 --- a/src/test/UploadTest.java +++ b/src/test/UploadTest.java @@ -1,27 +1,15 @@ package test; import com.em.miguelbridge.matrixbot.MatrixBot; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.http.entity.mime.HttpMultipartMode; -import org.apache.http.entity.mime.MultipartEntityBuilder; -import org.apache.http.entity.mime.content.FileBody; -import org.apache.http.impl.client.HttpClientBuilder; -import org.json.simple.JSONObject; import org.json.simple.parser.ParseException; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.io.InputStreamReader; import java.net.URISyntaxException; public class UploadTest { public static void main(String[] args) throws IOException, ParseException, URISyntaxException { + /* MatrixBot bot = new MatrixBot(); String token = bot.login(); String url = "https://maxwell.ydns.eu/_matrix/media/r0/" + @@ -51,5 +39,14 @@ public class UploadTest { result.append(line); System.out.println(response.getStatusLine() + " -e- " + result.toString()); + */ + + MatrixBot bot = new MatrixBot(); + String token = bot.login(); + bot.setAccessToken(token); + File file = new File("prova.png"); + //bot.sendMessage("provaa", "!mPkXwqjuGdhEVSopiG:maxwell.ydns.eu"); + + System.out.println(bot.sendFile("!mPkXwqjuGdhEVSopiG:maxwell.ydns.eu", file)); } }