From 1f986e92e0717e9106d500015283d557ac4ff28f Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 9 Oct 2020 14:22:28 +0300 Subject: [PATCH] osm stream file upload --- .../java/net/osmand/osm/io/NetworkUtils.java | 8 +++--- .../osm/oauth/IInputStreamHttpClient.java | 7 ------ .../osmand/osm/oauth/OsmAndJDKHttpClient.java | 25 +++++++++---------- .../oauth/OsmOAuthAuthorizationClient.java | 9 +++++-- 4 files changed, 22 insertions(+), 27 deletions(-) delete mode 100644 OsmAnd-java/src/main/java/net/osmand/osm/oauth/IInputStreamHttpClient.java diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/io/NetworkUtils.java b/OsmAnd-java/src/main/java/net/osmand/osm/io/NetworkUtils.java index d684b148a4..28a1ec3fb9 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/io/NetworkUtils.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/io/NetworkUtils.java @@ -4,7 +4,6 @@ import com.github.scribejava.core.model.OAuthRequest; import com.github.scribejava.core.model.Response; import com.github.scribejava.core.model.Verb; import net.osmand.PlatformUtil; -import net.osmand.osm.oauth.IInputStreamHttpClient; import net.osmand.osm.oauth.OsmOAuthAuthorizationClient; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -17,7 +16,7 @@ import java.util.zip.GZIPOutputStream; public class NetworkUtils { private static final Log log = PlatformUtil.getLog(NetworkUtils.class); - + private static final String GPX_UPLOAD_USER_AGENT = "OsmGPXUploadAgent"; private static Proxy proxy = null; public static String sendGetRequest(String urlText, String userNamePassword, StringBuilder responseBody){ @@ -76,9 +75,8 @@ public class NetworkUtils { client.getService().signRequest(client.getAccessToken(), req); req.addHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); try { - IInputStreamHttpClient service = (IInputStreamHttpClient) client.getService(); - service.execute(service.getUserAgent(), req.getHeaders(), req.getVerb(), req.getCompleteUrl(), fileToUpload); - Response r = client.getService().execute(req); + Response r = client.getHttpClient().execute(GPX_UPLOAD_USER_AGENT, req.getHeaders(), req.getVerb(), + req.getCompleteUrl(), fileToUpload); if (r.getCode() != 200) { return r.getBody(); } diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/IInputStreamHttpClient.java b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/IInputStreamHttpClient.java deleted file mode 100644 index c42182fdd7..0000000000 --- a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/IInputStreamHttpClient.java +++ /dev/null @@ -1,7 +0,0 @@ -package net.osmand.osm.oauth; - -import com.github.scribejava.core.httpclient.HttpClient; - -public interface IInputStreamHttpClient extends HttpClient { - String getUserAgent(); -} diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmAndJDKHttpClient.java b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmAndJDKHttpClient.java index c7d8f88446..9b320119ee 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmAndJDKHttpClient.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmAndJDKHttpClient.java @@ -19,7 +19,7 @@ import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -public class OsmAndJDKHttpClient implements IInputStreamHttpClient { +public class OsmAndJDKHttpClient implements HttpClient { private static final String BOUNDARY = "CowMooCowMooCowCowCow"; private final JDKHttpClientConfig config; @@ -72,8 +72,7 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient { OAuthRequest.ResponseConverter converter) { try { final Response response = doExecute(userAgent, headers, httpVerb, completeUrl, bodyType, bodyContents); - @SuppressWarnings("unchecked") - final T t = converter == null ? (T) response : converter.convert(response); + @SuppressWarnings("unchecked") final T t = converter == null ? (T) response : converter.convert(response); if (callback != null) { callback.onCompleted(t); } @@ -143,11 +142,6 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient { } } - @Override - public String getUserAgent() { - return "OsmandUserAgent"; - } - private enum BodyType { BYTE_ARRAY { @Override @@ -173,6 +167,7 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient { addBody(connection, ((String) bodyContents).getBytes(), requiresBody); } }; + abstract void setBody(HttpURLConnection connection, Object bodyContents, boolean requiresBody) throws IOException; } @@ -206,10 +201,14 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient { if (requiresBody) { String filename = file.getName(); String formName = "file"; - final OutputStream ous = prepareConnectionForBodyAndGetOutputStream(connection, 20 * 1024); - ous.write(("--" + BOUNDARY+"\r\n").getBytes()); InputStream stream = new FileInputStream(file); - ous.write(("content-disposition: form-data; name=\""+formName+"\"; filename=\"" + filename + "\"\r\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$ + 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-Type: application/octet-stream\r\n\r\n").getBytes()); //$NON-NLS-1$ BufferedInputStream bis = new BufferedInputStream(stream, 20 * 1024); ous.flush(); @@ -217,13 +216,13 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient { ous.write(("\r\n--" + BOUNDARY + "--\r\n").getBytes()); //$NON-NLS-1$ //$NON-NLS-2$ ous.flush(); Algorithms.closeStream(bis); - Algorithms.closeStream(ous); } } private static void addBody(HttpURLConnection connection, byte[] content, boolean requiresBody) throws IOException { final int contentLength = content.length; if (requiresBody || contentLength > 0) { + connection.setDoOutput(true); final OutputStream outputStream = prepareConnectionForBodyAndGetOutputStream(connection, contentLength); if (contentLength > 0) { outputStream.write(content); @@ -241,6 +240,7 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient { if (requiresBody) { final ByteArrayOutputStream os = MultipartUtils.getPayload(multipartPayload); final int contentLength = os.size(); + connection.setDoOutput(true); final OutputStream outputStream = prepareConnectionForBodyAndGetOutputStream(connection, contentLength); if (contentLength > 0) { os.writeTo(outputStream); @@ -254,7 +254,6 @@ public class OsmAndJDKHttpClient implements IInputStreamHttpClient { if (connection.getRequestProperty(CONTENT_TYPE) == null) { connection.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE); } - connection.setDoOutput(true); return connection.getOutputStream(); } } \ No newline at end of file diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java index 709dbaa275..f1c718465e 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/oauth/OsmOAuthAuthorizationClient.java @@ -4,7 +4,6 @@ package net.osmand.osm.oauth; import com.github.scribejava.core.builder.ServiceBuilder; import com.github.scribejava.core.builder.api.DefaultApi10a; 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.model.*; import com.github.scribejava.core.oauth.OAuth10aService; @@ -23,12 +22,14 @@ public class OsmOAuthAuthorizationClient { private OAuth1RequestToken requestToken; private OAuth1AccessToken accessToken; private final OAuth10aService service; + private final OsmAndJDKHttpClient httpClient; public final static Log log = PlatformUtil.getLog(OsmOAuthAuthorizationClient.class); public OsmOAuthAuthorizationClient(String key, String secret) { + httpClient = new OsmAndJDKHttpClient(JDKHttpClientConfig.defaultConfig()); service = new ServiceBuilder(key) .apiSecret(secret) - .httpClient(new OsmAndJDKHttpClient(JDKHttpClientConfig.defaultConfig())) + .httpClient(httpClient) .callback("osmand-oauth://example.com/oauth") .build(new OsmApi()); } @@ -55,6 +56,10 @@ public class OsmOAuthAuthorizationClient { } } + public OsmAndJDKHttpClient getHttpClient() { + return httpClient; + } + public OAuth10aService getService() { return service; }