Skip to content

Commit 055cd27

Browse files
Merge branch 'master' into release
2 parents e9aa1f0 + 507cecf commit 055cd27

File tree

165 files changed

+12314
-12169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+12314
-12169
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Add this dependency to your project's POM:
101101
<dependency>
102102
<groupId>com.aspose</groupId>
103103
<artifactId>aspose-words-cloud</artifactId>
104-
<version>20.9.0</version>
104+
<version>20.10.0</version>
105105
</dependency>
106106
</dependencies>
107107
```

pom.xml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<artifactId>aspose-words-cloud</artifactId>
55
<packaging>jar</packaging>
66
<name>AsposeWordsCloud</name>
7-
<version>20.9.0</version>
7+
<version>20.10.0</version>
88
<url>https://www.aspose.cloud/</url>
99
<description>Aspose Words Java SDK</description>
1010
<scm>
@@ -266,11 +266,16 @@
266266
<artifactId>gson-fire</artifactId>
267267
<version>${gson-fire-version}</version>
268268
</dependency>
269-
<dependency>
270-
<groupId>org.threeten</groupId>
271-
<artifactId>threetenbp</artifactId>
272-
<version>${threetenbp-version}</version>
273-
</dependency>
269+
<dependency>
270+
<groupId>javax.mail</groupId>
271+
<artifactId>mail</artifactId>
272+
<version>1.4.7</version>
273+
</dependency>
274+
<dependency>
275+
<groupId>org.threeten</groupId>
276+
<artifactId>threetenbp</artifactId>
277+
<version>${threetenbp-version}</version>
278+
</dependency>
274279
<!-- test dependencies -->
275280
<dependency>
276281
<groupId>junit</groupId>

src/main/java/com/aspose/words/cloud/ApiClient.java

Lines changed: 153 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
package com.aspose.words.cloud;
2929

30+
import com.aspose.words.cloud.model.requests.RequestIfc;
3031
import com.squareup.okhttp.*;
3132
import com.squareup.okhttp.internal.http.HttpMethod;
3233
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
@@ -36,6 +37,10 @@
3637
import org.threeten.bp.LocalDate;
3738
import org.threeten.bp.OffsetDateTime;
3839

40+
import javax.mail.BodyPart;
41+
import javax.mail.MessagingException;
42+
import javax.mail.internet.MimeMultipart;
43+
import javax.mail.util.ByteArrayDataSource;
3944
import java.io.*;
4045
import java.lang.reflect.Type;
4146
import java.net.URLConnection;
@@ -51,10 +56,12 @@ public class ApiClient {
5156
private String apiVersion = "v4.0";
5257
private String baseUrl = "https://api.aspose.cloud";
5358
private String basePath = baseUrl + "/" + apiVersion;
54-
private String clientVersion = "20.9";
59+
private String clientVersion = "20.10";
5560
private boolean debugging = false;
5661
private Map<String, String> defaultHeaderMap = new HashMap<String, String>();
5762
private String tempFolderPath = null;
63+
private Integer notAuthCode = 401;
64+
private Integer badRequestCode = 400;
5865

5966
private OkHttpClient httpClient;
6067
private JSON json;
@@ -92,6 +99,24 @@ public ApiClient() {
9299
setReadTimeout(5 * 60 * 1000);
93100
}
94101

102+
/**
103+
* Get NotAuth http code
104+
*
105+
* @return App Key
106+
*/
107+
public Integer getNotAuthCode() {
108+
return notAuthCode;
109+
}
110+
111+
/**
112+
* Get BadRequest http code
113+
*
114+
* @return App Key
115+
*/
116+
public Integer getBadRequestCode() {
117+
return badRequestCode;
118+
}
119+
95120
/**
96121
* Get App Key
97122
*
@@ -600,7 +625,17 @@ public <T> T deserialize(Response response, Type returnType) throws ApiException
600625
return null;
601626
}
602627

603-
if ("byte[]".equals(returnType.toString())) {
628+
if (returnType.equals(MimeMultipart.class)) {
629+
try {
630+
InputStream in = response.body().byteStream();
631+
ByteArrayDataSource dataSource = new ByteArrayDataSource(in, "multipart/form-data");
632+
return (T) new MimeMultipart(dataSource);
633+
}
634+
catch (IOException | MessagingException e) {
635+
throw new ApiException(e);
636+
}
637+
}
638+
else if (returnType.equals(byte[].class)) {
604639
// Handle binary response (byte array).
605640
try {
606641
return (T) response.body().bytes();
@@ -871,22 +906,9 @@ public <T> T handleResponse(Response response, Type returnType) throws ApiExcept
871906
/**
872907
* Build HTTP call with the given options.
873908
*
874-
* @param path The sub-path of the HTTP URL
875-
* @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE"
876-
* @param queryParams The query parameters
877-
* @param collectionQueryParams The collection query parameters
878-
* @param body The request body object
879-
* @param headerParams The header parameters
880-
* @param formParams The form parameters
881-
* @param authNames The authentications to apply
882-
* @param progressRequestListener Progress request listener
883-
* @return The HTTP call
884-
* @throws ApiException If fail to serialize the request body object
885-
* @throws IOException If fail to serialize the request body object
909+
* @param request The http request instance
886910
*/
887-
public Call buildCall(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException, IOException {
888-
Request request = buildRequest(path, method, queryParams, collectionQueryParams, body, headerParams, formParams, authNames, progressRequestListener);
889-
911+
public Call buildCall(Request request) {
890912
return httpClient.newCall(request);
891913
}
892914

@@ -906,11 +928,10 @@ public Call buildCall(String path, String method, List<Pair> queryParams, List<P
906928
* @throws ApiException If fail to serialize the request body object
907929
* @throws IOException If fail to serialize the request body object
908930
*/
909-
public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException, IOException {
910-
addOAuthAuthentication(headerParams);
911-
final String url = buildUrl(path, queryParams, collectionQueryParams);
912-
final Request.Builder reqBuilder = new Request.Builder().url(url);
913-
processHeaderParams(headerParams, reqBuilder);
931+
public Request buildRequest(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, Boolean addAuthHeaders, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException, IOException {
932+
if (addAuthHeaders) {
933+
addOAuthAuthentication(headerParams);
934+
}
914935

915936
String contentType = (String) headerParams.get("Content-Type");
916937
// ensuring a default content type
@@ -922,11 +943,17 @@ public Request buildRequest(String path, String method, List<Pair> queryParams,
922943
if (!HttpMethod.permitsRequestBody(method)) {
923944
reqBody = null;
924945
}
946+
else if (body instanceof RequestBody) {
947+
reqBody = (RequestBody) body;
948+
}
925949
else if ("application/x-www-form-urlencoded".equals(contentType)) {
926950
reqBody = buildRequestBodyFormEncoding(formParams);
927951
}
928952
else if ("multipart/form-data".equals(contentType)) {
929-
reqBody = buildRequestBodyMultipart(formParams);
953+
String boundary = UUID.randomUUID().toString();
954+
contentType += "; boundary=" + boundary;
955+
headerParams.put("Content-Type", contentType);
956+
reqBody = buildRequestBodyMultipart(formParams, boundary);
930957
}
931958
else if (body == null) {
932959
if ("DELETE".equals(method)) {
@@ -942,8 +969,11 @@ else if (body == null) {
942969
reqBody = serialize(body, contentType);
943970
}
944971

945-
Request request = null;
972+
final String url = buildUrl(path, queryParams, collectionQueryParams);
973+
final Request.Builder reqBuilder = new Request.Builder().url(url);
974+
processHeaderParams(headerParams, reqBuilder, addAuthHeaders);
946975

976+
Request request = null;
947977
if (progressRequestListener != null && reqBody != null) {
948978
ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener);
949979
request = reqBuilder.method(method, progressRequestBody).build();
@@ -1012,13 +1042,15 @@ public String buildUrl(String path, List<Pair> queryParams, List<Pair> collectio
10121042
* @param headerParams Header parameters in the ofrm of Map
10131043
* @param reqBuilder Reqeust.Builder
10141044
*/
1015-
public void processHeaderParams(Map<String, String> headerParams, Request.Builder reqBuilder) {
1045+
public void processHeaderParams(Map<String, String> headerParams, Request.Builder reqBuilder, Boolean addDefaultHeaders) {
10161046
for (Entry<String, String> param : headerParams.entrySet()) {
10171047
reqBuilder.header(param.getKey(), parameterToString(param.getValue()));
10181048
}
1019-
for (Entry<String, String> header : defaultHeaderMap.entrySet()) {
1020-
if (!headerParams.containsKey(header.getKey())) {
1021-
reqBuilder.header(header.getKey(), parameterToString(header.getValue()));
1049+
if (addDefaultHeaders) {
1050+
for (Entry<String, String> header : defaultHeaderMap.entrySet()) {
1051+
if (!headerParams.containsKey(header.getKey())) {
1052+
reqBuilder.header(header.getKey(), parameterToString(header.getValue()));
1053+
}
10221054
}
10231055
}
10241056
}
@@ -1045,8 +1077,8 @@ public RequestBody buildRequestBodyFormEncoding(Map<String, Object> formParams)
10451077
* @throws IOException If fail to serialize the request body object
10461078
* @return RequestBody
10471079
*/
1048-
public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams) throws IOException {
1049-
MultipartBuilder mpBuilder = new MultipartBuilder().type(MultipartBuilder.FORM);
1080+
public RequestBody buildRequestBodyMultipart(Map<String, Object> formParams, String boundary) throws IOException {
1081+
MultipartBuilder mpBuilder = new MultipartBuilder(boundary).type(MultipartBuilder.FORM);
10501082
if (formParams.isEmpty()) {
10511083
Headers partHeaders = Headers.of("Content-Disposition", "form-data");
10521084
mpBuilder.addPart(partHeaders, RequestBody.create(MediaType.parse("none"), new byte[] {}));
@@ -1113,6 +1145,97 @@ public void requestToken() throws ApiException {
11131145
}
11141146
}
11151147

1148+
/**
1149+
* AddParameterToQuery
1150+
*/
1151+
public void addParameterToQuery(List<Pair> queryParams, String paramName, Object paramValue) {
1152+
queryParams.addAll(parameterToPair(paramName, paramValue));
1153+
}
1154+
1155+
/**
1156+
* AddParameterToPath
1157+
*/
1158+
public String addParameterToPath(String path, String paramName, Object paramValue) {
1159+
if (path.contains("{" + paramName + "}")) {
1160+
if (paramValue == null || paramValue.equals("")) {
1161+
return path.replace("{" + paramName + "}", "");
1162+
}
1163+
else {
1164+
return path.replace("{" + paramName + "}", paramValue.toString());
1165+
}
1166+
}
1167+
1168+
return path;
1169+
}
1170+
1171+
/**
1172+
* Build batch request
1173+
*/
1174+
public Request buildBatchRequest(RequestIfc[] requests) throws ApiException, IOException {
1175+
Headers multipartHeaders = Headers.of("Content-Disposition", "form-data");
1176+
MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);
1177+
for (RequestIfc request : requests) {
1178+
Request httpRequest = request.buildHttpRequest(this, null, null, false);
1179+
builder.addPart(multipartHeaders, new ChildRequestContent(httpRequest, basePath + "/words/"));
1180+
}
1181+
1182+
RequestBody requestBody = builder.build();
1183+
Map<String, String> headers = new HashMap<>();
1184+
headers.put("Content-Type", requestBody.contentType().toString());
1185+
return buildRequest("/words/batch", "PUT", new ArrayList<>(), new ArrayList<>(), requestBody, headers, new HashMap<>(), true, null);
1186+
}
1187+
1188+
/**
1189+
* Parse batch part
1190+
*/
1191+
public Object parseBatchPart(Request masterRequest, BodyPart bodyPart, Type returnType) throws IOException, MessagingException, ApiException {
1192+
InputStream is = bodyPart.getInputStream();
1193+
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
1194+
1195+
int nRead;
1196+
byte[] data = new byte[16384];
1197+
1198+
while ((nRead = is.read(data, 0, data.length)) != -1) {
1199+
buffer.write(data, 0, nRead);
1200+
}
1201+
1202+
try {
1203+
String stringData = buffer.toString("UTF-8");
1204+
int lastSplitIndex = stringData.indexOf("\r\n");
1205+
Integer responseCode = Integer.parseInt(stringData.substring(0, lastSplitIndex).split("\\s+")[0]);
1206+
if (responseCode != 200) {
1207+
return new ApiException(responseCode, stringData);
1208+
}
1209+
1210+
Headers.Builder headersBuilder = new Headers.Builder();
1211+
while (true) {
1212+
int splitIndex = stringData.indexOf("\r\n", lastSplitIndex + 2);
1213+
String headerStr = stringData.substring(lastSplitIndex + 2, splitIndex);
1214+
lastSplitIndex = splitIndex;
1215+
1216+
if (headerStr.isEmpty()) {
1217+
break;
1218+
}
1219+
1220+
headersBuilder.add(headerStr);
1221+
}
1222+
1223+
ResponseBody responseBody = null;
1224+
Headers headers = headersBuilder.build();
1225+
byte[] rawBody = buffer.toByteArray();
1226+
if (rawBody.length != lastSplitIndex + 2) {
1227+
byte[] responseBytes = Arrays.copyOfRange(rawBody, lastSplitIndex + 2, rawBody.length);
1228+
responseBody = ResponseBody.create(MediaType.parse(headers.get("Content-Type")), responseBytes);
1229+
}
1230+
1231+
Response response = new Response.Builder().request(masterRequest).protocol(Protocol.HTTP_1_1).code(responseCode).headers(headers).body(responseBody).build();
1232+
return deserialize(response, returnType);
1233+
}
1234+
catch (Exception e) {
1235+
throw new ApiException(400, "Invalid response format.");
1236+
}
1237+
}
1238+
11161239
/**
11171240
* Add OAuth2 header
11181241
*
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* --------------------------------------------------------------------------------
3+
* <copyright company="Aspose" file="ChildRequestContent.java">
4+
* Copyright (c) 2020 Aspose.Words for Cloud
5+
* </copyright>
6+
* <summary>
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
* </summary>
25+
* --------------------------------------------------------------------------------
26+
*/
27+
28+
package com.aspose.words.cloud;
29+
30+
import com.squareup.okhttp.MediaType;
31+
import com.squareup.okhttp.Request;
32+
import com.squareup.okhttp.RequestBody;
33+
import okio.BufferedSink;
34+
35+
import java.io.IOException;
36+
import java.net.URI;
37+
38+
public class ChildRequestContent extends RequestBody {
39+
private static final String CRLF = "\r\n";
40+
private Request request;
41+
private URI baseUri;
42+
43+
public ChildRequestContent(Request request, String rootUrl) {
44+
this.request = request;
45+
this.baseUri = URI.create(rootUrl);
46+
}
47+
48+
@Override
49+
public MediaType contentType() {
50+
return MediaType.parse("application/http; msgtype=request");
51+
}
52+
53+
@Override
54+
public void writeTo(BufferedSink bufferedSink) throws IOException {
55+
bufferedSink.writeUtf8(request.method());
56+
bufferedSink.writeUtf8(" ");
57+
bufferedSink.writeUtf8(baseUri.relativize(request.uri()).toString());
58+
bufferedSink.writeUtf8(" ");
59+
bufferedSink.writeUtf8(CRLF);
60+
61+
for (String key : request.headers().names()) {
62+
bufferedSink.writeUtf8(key);
63+
bufferedSink.writeUtf8(": ");
64+
bufferedSink.writeUtf8(request.headers().get(key));
65+
bufferedSink.writeUtf8(CRLF);
66+
}
67+
68+
bufferedSink.writeUtf8(CRLF);
69+
if (request.body() != null) {
70+
request.body().writeTo(bufferedSink);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)