move started

This commit is contained in:
simon 2020-11-16 19:16:06 +02:00
parent 95ef1c7436
commit a0534c7232
4 changed files with 159 additions and 0 deletions

View file

@ -56,6 +56,55 @@ public class NetworkUtils {
return e.getMessage(); return e.getMessage();
} }
} }
public static String sendPostDataRequest(String urlText, InputStream data) {
try {
log.info("POST : " + urlText);
HttpURLConnection conn = getHttpURLConnection(urlText);
conn.setDoInput(true);
conn.setDoOutput(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("User-Agent", "OsmAnd"); //$NON-NLS-1$ //$NON-NLS-2$
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
OutputStream ous = conn.getOutputStream();
ous.write(("--" + BOUNDARY + "\r\n").getBytes());
ous.write(("content-disposition: form-data; name=\"" + "file" + "\"; filename=\"" + "image1" + "\"\r\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
ous.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes()); //$NON-NLS-1$
Algorithms.streamCopy(data, ous);
ous.write(("\r\n--" + BOUNDARY + "--\r\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
ous.flush();
log.info("Response code and message : " + conn.getResponseCode() + " " + conn.getResponseMessage());
if (conn.getResponseCode() != 200) {
return null;
}
StringBuilder responseBody = new StringBuilder();
InputStream is = conn.getInputStream();
responseBody.setLength(0);
if (is != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); //$NON-NLS-1$
String s;
boolean first = true;
while ((s = in.readLine()) != null) {
if (first) {
first = false;
} else {
responseBody.append("\n"); //$NON-NLS-1$
}
responseBody.append(s);
}
is.close();
}
Algorithms.closeStream(is);
Algorithms.closeStream(data);
Algorithms.closeStream(ous);
return responseBody.toString();
} catch (IOException e) {
log.error(e.getMessage(), e);
return e.getMessage();
}
}
private static final String BOUNDARY = "CowMooCowMooCowCowCow"; //$NON-NLS-1$ private static final String BOUNDARY = "CowMooCowMooCowCowCow"; //$NON-NLS-1$
public static String uploadFile(String urlText, File fileToUpload, String userNamePassword, public static String uploadFile(String urlText, File fileToUpload, String userNamePassword,
OsmOAuthAuthorizationClient client, OsmOAuthAuthorizationClient client,

View file

@ -526,4 +526,6 @@ dependencies {
implementation 'com.jaredrummler:colorpicker:1.1.0' implementation 'com.jaredrummler:colorpicker:1.1.0'
freehuaweiImplementation 'com.huawei.hms:iap:5.0.2.300' freehuaweiImplementation 'com.huawei.hms:iap:5.0.2.300'
implementation "org.bouncycastle:bcpkix-jdk15on:1.56"
} }

View file

@ -509,6 +509,7 @@ public abstract class ImageCard extends AbstractCard {
"Requesting location images...", false, false); "Requesting location images...", false, false);
try { try {
if (!Algorithms.isEmpty(response)) { if (!Algorithms.isEmpty(response)) {
//TODO extract place id
JSONArray obj = new JSONObject(response).getJSONArray("objects"); JSONArray obj = new JSONObject(response).getJSONArray("objects");
JSONArray images = ((JSONObject) ((JSONObject) obj.get(0)).get("images")).getJSONArray("outdoor"); JSONArray images = ((JSONObject) ((JSONObject) obj.get(0)).get("images")).getJSONArray("outdoor");
if (images.length() > 0) { if (images.length() > 0) {

View file

@ -0,0 +1,107 @@
package net.osmand.plus.osmedit.opr;
import android.net.TrafficStats;
import android.os.Build;
import com.google.gson.GsonBuilder;
import net.osmand.PlatformUtil;
import net.osmand.plus.BuildConfig;
import org.apache.commons.logging.Log;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.openplacereviews.opendb.SecUtils;
import org.openplacereviews.opendb.ops.OpOperation;
import org.openplacereviews.opendb.util.JsonFormatter;
import org.openplacereviews.opendb.util.exception.FailedVerificationException;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyPair;
import java.security.Security;
import java.util.*;
import static org.openplacereviews.opendb.SecUtils.*;
public class OpenDBAPI {
private static final Log log = PlatformUtil.getLog(SecUtils.class);
private static final int THREAD_ID = 11200;
public int uploadImage(String[] placeId, String privateKey, String username, String image) throws FailedVerificationException {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Security.removeProvider("BC");
Security.addProvider(new BouncyCastleProvider());
}
KeyPair kp = SecUtils.getKeyPair(ALGO_EC, privateKey, null);
String signed = username;// + ":opr-web";
JsonFormatter formatter = new JsonFormatter();
OPRImage OPRImage = new GsonBuilder().create().fromJson(image, OPRImage.class);
OpOperation opOperation = new OpOperation();
opOperation.setType("opr.place");
List<Object> edits = new ArrayList<>();
Map<String, Object> edit = new TreeMap<>();
List<Object> imageResponseList = new ArrayList<>();
Map<String, Object> imageMap = new TreeMap<>();
imageMap.put("cid", OPRImage.cid);
imageMap.put("hash", OPRImage.hash);
imageMap.put("extension", OPRImage.extension);
imageMap.put("type", OPRImage.type);
imageResponseList.add(imageMap);
List<String> ids = new ArrayList<>(Arrays.asList(placeId));
Map<String, Object> change = new TreeMap<>();
Map<String, Object> images = new TreeMap<>();
Map<String, Object> outdoor = new TreeMap<>();
outdoor.put("outdoor", imageResponseList);
images.put("append", outdoor);
change.put("version", "increment");
change.put("images", images);
edit.put("id", ids);
edit.put("change", change);
edit.put("current", new Object());
edits.add(edit);
opOperation.putObjectValue(OpOperation.F_EDIT, edits);
opOperation.setSignedBy(signed);
String hash = JSON_MSG_TYPE + ":"
+ SecUtils.calculateHashWithAlgo(SecUtils.HASH_SHA256, null,
formatter.opToJsonNoHash(opOperation));
byte[] hashBytes = SecUtils.getHashBytes(hash);
String signature = signMessageWithKeyBase64(kp, hashBytes, SecUtils.SIG_ALGO_SHA1_EC, null);
opOperation.addOrSetStringValue("hash", hash);
opOperation.addOrSetStringValue("signature", signature);
TrafficStats.setThreadStatsTag(THREAD_ID);
String url = BuildConfig.OPR_BASE_URL + "api/auth/process-operation?addToQueue=true&dontSignByServer=false";
String json = formatter.opToJson(opOperation);
System.out.println("JSON: " + json);
HttpURLConnection connection;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(10000);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
try {
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(json.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
int rc = connection.getResponseCode();
if (rc != 200) {
log.error("ERROR HAPPENED");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
log.error(strCurrentLine);
}
}
return rc;
} catch (IOException e) {
log.error(e);
}
return -1;
}
}