osm stream file upload

This commit is contained in:
simon 2020-10-09 14:22:28 +03:00
parent 0f3070f160
commit 1f986e92e0
4 changed files with 22 additions and 27 deletions

View file

@ -4,7 +4,6 @@ import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response; import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb; import com.github.scribejava.core.model.Verb;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.osm.oauth.IInputStreamHttpClient;
import net.osmand.osm.oauth.OsmOAuthAuthorizationClient; import net.osmand.osm.oauth.OsmOAuthAuthorizationClient;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -17,7 +16,7 @@ import java.util.zip.GZIPOutputStream;
public class NetworkUtils { public class NetworkUtils {
private static final Log log = PlatformUtil.getLog(NetworkUtils.class); private static final Log log = PlatformUtil.getLog(NetworkUtils.class);
private static final String GPX_UPLOAD_USER_AGENT = "OsmGPXUploadAgent";
private static Proxy proxy = null; private static Proxy proxy = null;
public static String sendGetRequest(String urlText, String userNamePassword, StringBuilder responseBody){ public static String sendGetRequest(String urlText, String userNamePassword, StringBuilder responseBody){
@ -76,9 +75,8 @@ public class NetworkUtils {
client.getService().signRequest(client.getAccessToken(), req); client.getService().signRequest(client.getAccessToken(), req);
req.addHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); req.addHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
try { try {
IInputStreamHttpClient service = (IInputStreamHttpClient) client.getService(); Response r = client.getHttpClient().execute(GPX_UPLOAD_USER_AGENT, req.getHeaders(), req.getVerb(),
service.execute(service.getUserAgent(), req.getHeaders(), req.getVerb(), req.getCompleteUrl(), fileToUpload); req.getCompleteUrl(), fileToUpload);
Response r = client.getService().execute(req);
if (r.getCode() != 200) { if (r.getCode() != 200) {
return r.getBody(); return r.getBody();
} }

View file

@ -1,7 +0,0 @@
package net.osmand.osm.oauth;
import com.github.scribejava.core.httpclient.HttpClient;
public interface IInputStreamHttpClient extends HttpClient {
String getUserAgent();
}

View file

@ -19,7 +19,7 @@ import java.util.Map;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future; import java.util.concurrent.Future;
public class OsmAndJDKHttpClient implements IInputStreamHttpClient { public class OsmAndJDKHttpClient implements HttpClient {
private static final String BOUNDARY = "CowMooCowMooCowCowCow"; private static final String BOUNDARY = "CowMooCowMooCowCowCow";
private final JDKHttpClientConfig config; private final JDKHttpClientConfig config;
@ -72,8 +72,7 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient {
OAuthRequest.ResponseConverter<T> converter) { OAuthRequest.ResponseConverter<T> converter) {
try { try {
final Response response = doExecute(userAgent, headers, httpVerb, completeUrl, bodyType, bodyContents); final Response response = doExecute(userAgent, headers, httpVerb, completeUrl, bodyType, bodyContents);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked") final T t = converter == null ? (T) response : converter.convert(response);
final T t = converter == null ? (T) response : converter.convert(response);
if (callback != null) { if (callback != null) {
callback.onCompleted(t); callback.onCompleted(t);
} }
@ -143,11 +142,6 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient {
} }
} }
@Override
public String getUserAgent() {
return "OsmandUserAgent";
}
private enum BodyType { private enum BodyType {
BYTE_ARRAY { BYTE_ARRAY {
@Override @Override
@ -173,6 +167,7 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient {
addBody(connection, ((String) bodyContents).getBytes(), requiresBody); addBody(connection, ((String) bodyContents).getBytes(), requiresBody);
} }
}; };
abstract void setBody(HttpURLConnection connection, Object bodyContents, boolean requiresBody) abstract void setBody(HttpURLConnection connection, Object bodyContents, boolean requiresBody)
throws IOException; throws IOException;
} }
@ -206,9 +201,13 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient {
if (requiresBody) { if (requiresBody) {
String filename = file.getName(); String filename = file.getName();
String formName = "file"; String formName = "file";
final OutputStream ous = prepareConnectionForBodyAndGetOutputStream(connection, 20 * 1024);
ous.write(("--" + BOUNDARY+"\r\n").getBytes());
InputStream stream = new FileInputStream(file); InputStream stream = new FileInputStream(file);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); //$NON-NLS-1$ //$NON-NLS-2$
connection.setRequestProperty("User-Agent", "OsmAnd"); //$NON-NLS-1$ //$NON-NLS-2$
final OutputStream ous = connection.getOutputStream();
ous.write(("--" + BOUNDARY + "\r\n").getBytes());
ous.write(("content-disposition: form-data; name=\"" + formName + "\"; filename=\"" + filename + "\"\r\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$ ous.write(("content-disposition: form-data; name=\"" + formName + "\"; filename=\"" + filename + "\"\r\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
ous.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes()); //$NON-NLS-1$ ous.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes()); //$NON-NLS-1$
BufferedInputStream bis = new BufferedInputStream(stream, 20 * 1024); BufferedInputStream bis = new BufferedInputStream(stream, 20 * 1024);
@ -217,13 +216,13 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient {
ous.write(("\r\n--" + BOUNDARY + "--\r\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$ ous.write(("\r\n--" + BOUNDARY + "--\r\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$
ous.flush(); ous.flush();
Algorithms.closeStream(bis); Algorithms.closeStream(bis);
Algorithms.closeStream(ous);
} }
} }
private static void addBody(HttpURLConnection connection, byte[] content, boolean requiresBody) throws IOException { private static void addBody(HttpURLConnection connection, byte[] content, boolean requiresBody) throws IOException {
final int contentLength = content.length; final int contentLength = content.length;
if (requiresBody || contentLength > 0) { if (requiresBody || contentLength > 0) {
connection.setDoOutput(true);
final OutputStream outputStream = prepareConnectionForBodyAndGetOutputStream(connection, contentLength); final OutputStream outputStream = prepareConnectionForBodyAndGetOutputStream(connection, contentLength);
if (contentLength > 0) { if (contentLength > 0) {
outputStream.write(content); outputStream.write(content);
@ -241,6 +240,7 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient {
if (requiresBody) { if (requiresBody) {
final ByteArrayOutputStream os = MultipartUtils.getPayload(multipartPayload); final ByteArrayOutputStream os = MultipartUtils.getPayload(multipartPayload);
final int contentLength = os.size(); final int contentLength = os.size();
connection.setDoOutput(true);
final OutputStream outputStream = prepareConnectionForBodyAndGetOutputStream(connection, contentLength); final OutputStream outputStream = prepareConnectionForBodyAndGetOutputStream(connection, contentLength);
if (contentLength > 0) { if (contentLength > 0) {
os.writeTo(outputStream); os.writeTo(outputStream);
@ -254,7 +254,6 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient {
if (connection.getRequestProperty(CONTENT_TYPE) == null) { if (connection.getRequestProperty(CONTENT_TYPE) == null) {
connection.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE); connection.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
} }
connection.setDoOutput(true);
return connection.getOutputStream(); return connection.getOutputStream();
} }
} }

View file

@ -4,7 +4,6 @@ package net.osmand.osm.oauth;
import com.github.scribejava.core.builder.ServiceBuilder; import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.builder.api.DefaultApi10a; import com.github.scribejava.core.builder.api.DefaultApi10a;
import com.github.scribejava.core.builder.api.OAuth1SignatureType; import com.github.scribejava.core.builder.api.OAuth1SignatureType;
import com.github.scribejava.core.httpclient.jdk.JDKHttpClient;
import com.github.scribejava.core.httpclient.jdk.JDKHttpClientConfig; import com.github.scribejava.core.httpclient.jdk.JDKHttpClientConfig;
import com.github.scribejava.core.model.*; import com.github.scribejava.core.model.*;
import com.github.scribejava.core.oauth.OAuth10aService; import com.github.scribejava.core.oauth.OAuth10aService;
@ -23,12 +22,14 @@ public class OsmOAuthAuthorizationClient {
private OAuth1RequestToken requestToken; private OAuth1RequestToken requestToken;
private OAuth1AccessToken accessToken; private OAuth1AccessToken accessToken;
private final OAuth10aService service; private final OAuth10aService service;
private final OsmAndJDKHttpClient httpClient;
public final static Log log = PlatformUtil.getLog(OsmOAuthAuthorizationClient.class); public final static Log log = PlatformUtil.getLog(OsmOAuthAuthorizationClient.class);
public OsmOAuthAuthorizationClient(String key, String secret) { public OsmOAuthAuthorizationClient(String key, String secret) {
httpClient = new OsmAndJDKHttpClient(JDKHttpClientConfig.defaultConfig());
service = new ServiceBuilder(key) service = new ServiceBuilder(key)
.apiSecret(secret) .apiSecret(secret)
.httpClient(new OsmAndJDKHttpClient(JDKHttpClientConfig.defaultConfig())) .httpClient(httpClient)
.callback("osmand-oauth://example.com/oauth") .callback("osmand-oauth://example.com/oauth")
.build(new OsmApi()); .build(new OsmApi());
} }
@ -55,6 +56,10 @@ public class OsmOAuthAuthorizationClient {
} }
} }
public OsmAndJDKHttpClient getHttpClient() {
return httpClient;
}
public OAuth10aService getService() { public OAuth10aService getService() {
return service; return service;
} }