osm stream file upload
This commit is contained in:
parent
0f3070f160
commit
1f986e92e0
4 changed files with 22 additions and 27 deletions
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package net.osmand.osm.oauth;
|
||||
|
||||
import com.github.scribejava.core.httpclient.HttpClient;
|
||||
|
||||
public interface IInputStreamHttpClient extends HttpClient {
|
||||
String getUserAgent();
|
||||
}
|
|
@ -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<T> 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue