1
1
mirror of https://github.com/ahabhyde/miguelbridge synced 2025-01-10 06:24:20 +01:00

Aggiunta richiesta post di upload file funzionante

This commit is contained in:
Ahab Hyde 2018-04-20 15:29:44 +02:00
parent c4bce1ac74
commit 0f305fb327
3 changed files with 73 additions and 43 deletions

View File

@ -76,12 +76,19 @@ public class MatrixBot {
*/ */
public String login() throws IOException, ParseException, URISyntaxException { public String login() throws IOException, ParseException, URISyntaxException {
String requestUrl = homeUrl + "client/r0/login"; String requestUrl = homeUrl + "client/r0/login";
/*
String[][] reqParams = new String[][] { String[][] reqParams = new String[][] {
{"type", "m.login.password"}, {"type", "m.login.password"},
{"user", readBotUserName()}, {"user", readBotUserName()},
{"password", readPswd()} {"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); 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", String requestUrl = homeUrl + String.format("client/r0/rooms/%s/join?access_token=%s",
roomAddress, accessToken); roomAddress, accessToken);
String[][] reqParams = null; JSONObject reqParams = new JSONObject();
String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams); String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams);
return risposta[0]; return risposta[0];
@ -103,21 +110,55 @@ public class MatrixBot {
public synchronized String sendMessage(String message, String roomAddress) throws IOException, URISyntaxException { 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", String requestUrl = homeUrl + String.format("client/r0/rooms/%s/send/m.room.message?access_token=%s",
roomAddress, accessToken); roomAddress, accessToken);
/*
String[][] reqParams = new String[][] { String[][] reqParams = new String[][] {
{"msgtype", "m.text"}, {"msgtype", "m.text"},
{"body", message} {"body", message}
}; };
*/
JSONObject reqParams = new JSONObject();
reqParams.put("msgtype", "m.text");
reqParams.put("body", message);
String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams); String[] risposta = RequestHandler.postRequestJSON(requestUrl, reqParams);
return risposta[0] + " - " + risposta[1]; 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", String requestUrl = homeUrl + String.format("media/r0/upload?filename=%s&access_token=%s",
file.getName(), accessToken); file.getName(), accessToken);
String[] risposta = RequestHandler.postRequestFile(requestUrl, file); 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]; return risposta[0] + " - " + risposta[1];
} }

View File

@ -1,9 +1,6 @@
package com.em.miguelbridge.matrixbot; package com.em.miguelbridge.matrixbot;
import java.io.BufferedReader; import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -13,13 +10,18 @@ import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; 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.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody; 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.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
/** /**
@ -48,24 +50,16 @@ public class RequestHandler {
return risposta; 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(); HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(inUrl); HttpPost post = new HttpPost(inUrl);
//add header //add header
post.setHeader("User-Agent", "Mozilla/5.0"); post.setHeader("User-Agent", "Mozilla/5.0");
post.addHeader("Content-Type", "charset=UTF_8"); 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(); String jsonString = inObj.toJSONString();
StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
StringEntity requestEntity = new StringEntity(obj.toJSONString(), ContentType.APPLICATION_JSON);
post.setEntity(requestEntity); post.setEntity(requestEntity);
HttpResponse response = client.execute(post); HttpResponse response = client.execute(post);
@ -84,19 +78,17 @@ public class RequestHandler {
} }
public static String[] postRequestFile(String inUrl, File file) throws IOException { 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(); byte[] b = new byte[(int) file.length()];
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); FileInputStream fileInputStream = new FileInputStream(file);
builder.addPart("content", fileBody); fileInputStream.read(b);
HttpEntity entity = builder.build();
HttpPost request = new HttpPost(inUrl);
request.setEntity(entity);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
HttpEntity entity = new ByteArrayEntity(b);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader rd = new BufferedReader( BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent())); new InputStreamReader(response.getEntity().getContent()));

View File

@ -1,27 +1,15 @@
package test; package test;
import com.em.miguelbridge.matrixbot.MatrixBot; 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 org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException; import java.net.URISyntaxException;
public class UploadTest { public class UploadTest {
public static void main(String[] args) throws IOException, ParseException, URISyntaxException { public static void main(String[] args) throws IOException, ParseException, URISyntaxException {
/*
MatrixBot bot = new MatrixBot(); MatrixBot bot = new MatrixBot();
String token = bot.login(); String token = bot.login();
String url = "https://maxwell.ydns.eu/_matrix/media/r0/" + String url = "https://maxwell.ydns.eu/_matrix/media/r0/" +
@ -51,5 +39,14 @@ public class UploadTest {
result.append(line); result.append(line);
System.out.println(response.getStatusLine() + " -e- " + result.toString()); 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));
} }
} }