Search in sources :

Example 1 with Collectors.toMap

use of oap.util.Maps.Collectors.toMap in project oap by oaplatform.

the class Client method uploadFile.

@SneakyThrows
public Response uploadFile(String uri, String prefix, Path path) {
    final ContentType contentType = ContentType.create(java.nio.file.Files.probeContentType(path));
    OkHttpClient client = new OkHttpClient();
    final MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("upfile", path.toFile().getName(), RequestBody.create(MediaType.parse(contentType.toString()), path.toFile())).addFormDataPart("prefix", prefix).build();
    okhttp3.Request request = new okhttp3.Request.Builder().url(uri).post(body).build();
    final okhttp3.Response response = client.newCall(request).execute();
    final Headers headers = response.headers();
    final java.util.stream.Stream<String> stream = headers.names().stream();
    final Map<String, String> h = stream.collect(Collectors.toMap(n -> n, headers::get));
    return new Response(response.code(), response.message(), h, Optional.ofNullable(response.body().contentType()).map(mt -> ContentType.create(mt.type() + "/" + mt.subtype(), mt.charset())), response.body().bytes());
}
Also used : Binder(oap.json.Binder) HttpPost(org.apache.http.client.methods.HttpPost) SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) BufferedInputStream(java.io.BufferedInputStream) IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) SneakyThrows(lombok.SneakyThrows) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Maps(oap.util.Maps) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) RequestConfig(org.apache.http.client.config.RequestConfig) HttpAsyncClients(org.apache.http.impl.nio.client.HttpAsyncClients) Header(org.apache.http.Header) HTTP_NOT_MODIFIED(java.net.HttpURLConnection.HTTP_NOT_MODIFIED) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) Future(java.util.concurrent.Future) SSLContexts(org.apache.http.ssl.SSLContexts) Map(java.util.Map) ToString(lombok.ToString) TypeRef(oap.reflect.TypeRef) NoopIOSessionStrategy(org.apache.http.nio.conn.NoopIOSessionStrategy) URI(java.net.URI) Stream(oap.util.Stream) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) Closeables(oap.io.Closeables) PoolingNHttpClientConnectionManager(org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ProgressInputStream.progress(oap.io.ProgressInputStream.progress) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) DefaultConnectionKeepAliveStrategy(org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) KeyStore(java.security.KeyStore) PublicSuffixMatcherLoader(org.apache.http.conn.util.PublicSuffixMatcherLoader) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Slf4j(lombok.extern.slf4j.Slf4j) Try(oap.util.Try) List(java.util.List) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) MultipartBody(okhttp3.MultipartBody) HttpGet(org.apache.http.client.methods.HttpGet) ByteStreams(com.google.common.io.ByteStreams) DefaultHostnameVerifier(org.apache.http.conn.ssl.DefaultHostnameVerifier) Optional(java.util.Optional) NameValuePair(org.apache.http.NameValuePair) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Pair(oap.util.Pair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DateUtils(org.apache.http.client.utils.DateUtils) RegistryBuilder(org.apache.http.config.RegistryBuilder) Pair.__(oap.util.Pair.__) Files(oap.io.Files) CookieSpecs(org.apache.http.client.config.CookieSpecs) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Headers(okhttp3.Headers) RequestBody(okhttp3.RequestBody) HTTP_OK(java.net.HttpURLConnection.HTTP_OK) HttpDelete(org.apache.http.client.methods.HttpDelete) Collectors.toMap(oap.util.Maps.Collectors.toMap) Cookie(org.apache.http.cookie.Cookie) StringUtils.split(org.apache.commons.lang3.StringUtils.split) SchemeIOSessionStrategy(org.apache.http.nio.conn.SchemeIOSessionStrategy) lombok.val(lombok.val) AsyncCallbacks(oap.concurrent.AsyncCallbacks) FutureCallback(org.apache.http.concurrent.FutureCallback) IOException(java.io.IOException) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) IOReactorException(org.apache.http.nio.reactor.IOReactorException) DefaultConnectingIOReactor(org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor) HTTP_MOVED_TEMP(java.net.HttpURLConnection.HTTP_MOVED_TEMP) OkHttpClient(okhttp3.OkHttpClient) HttpPut(org.apache.http.client.methods.HttpPut) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) IoStreams(oap.io.IoStreams) PLAIN(oap.io.IoStreams.Encoding.PLAIN) Closeable(java.io.Closeable) HttpResponse(org.apache.http.HttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity) InputStream(java.io.InputStream) APPLICATION_OCTET_STREAM(org.apache.http.entity.ContentType.APPLICATION_OCTET_STREAM) OkHttpClient(okhttp3.OkHttpClient) ContentType(org.apache.http.entity.ContentType) Headers(okhttp3.Headers) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) RegistryBuilder(org.apache.http.config.RegistryBuilder) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ToString(lombok.ToString) HttpResponse(org.apache.http.HttpResponse) MultipartBody(okhttp3.MultipartBody) SneakyThrows(lombok.SneakyThrows)

Aggregations

ByteStreams (com.google.common.io.ByteStreams)1 BufferedInputStream (java.io.BufferedInputStream)1 Closeable (java.io.Closeable)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HTTP_MOVED_TEMP (java.net.HttpURLConnection.HTTP_MOVED_TEMP)1 HTTP_NOT_MODIFIED (java.net.HttpURLConnection.HTTP_NOT_MODIFIED)1 HTTP_OK (java.net.HttpURLConnection.HTTP_OK)1 URI (java.net.URI)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Path (java.nio.file.Path)1 KeyStore (java.security.KeyStore)1 Arrays (java.util.Arrays)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1