Skip to content

Commit 8dd5591

Browse files
Merge pull request #58 from adityagai/master
Removed credentials
2 parents bfba5dc + a8d7e1a commit 8dd5591

File tree

11 files changed

+147
-148
lines changed

11 files changed

+147
-148
lines changed

examples/java/com/ibm/watson/developer_cloud/document_conversion/v1/DocumentConversionExample.java

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,48 +39,41 @@ public static void main(String[] args) throws URISyntaxException, IOException, I
3939

4040
System.out.println("-------------------- Convert html document to Answers ----------------------");
4141
Answers htmlToAnswers = service.convertDocumentToAnswer(html);
42-
System.out.println("HTML converted to Answers:\n" + htmlToAnswers);
42+
System.out.println(htmlToAnswers);
4343

4444
System.out.println("-------------------- Convert html document to Normalized HTML ----------------------");
4545
String htmlToNormHtml = ConversionUtils.writeInputStreamToString(service.convertDocument(html, ConversionTarget.NORMALIZED_HTML));
46-
System.out.println("HTML converted to Normalized HTML:\n" + htmlToNormHtml);
46+
System.out.println(htmlToNormHtml);
4747

4848
System.out.println("-------------------- Convert html document to Normalized Text ----------------------");
4949
String htmlToNormText = ConversionUtils.writeInputStreamToString(service.convertDocument(html, ConversionTarget.NORMALIZED_TEXT));
50-
51-
System.out.println("HTML converted to Normalized Text:\n" + htmlToNormText);
50+
System.out.println(htmlToNormText);
5251

5352
System.out.println("-------------------- Convert pdf document to Answers ----------------------");
5453
Answers pdfToAnswers = service.convertDocumentToAnswer(pdf);
55-
System.out.println("PDF converted to Answers:\n" + pdfToAnswers);
54+
System.out.println(pdfToAnswers);
5655

5756
System.out.println("-------------------- Convert MS Word document to Answers ----------------------");
5857
Answers docToAnswers = service.convertDocumentToAnswer(doc);
59-
System.out.println("MS Word converted to Answers:\n" + docToAnswers);
58+
System.out.println(docToAnswers);
6059

6160
// ## Scenario 2: Convert a document using a batch operation ##
6261
// Step 1. Upload a document
6362
System.out.println("-------------------- Upload a document ------------------------------");
6463
File tempFile1 = new File("src/test/resources/document_conversion/pdf-with-sections-input.pdf");
6564
Document doc1 = service.uploadDocument(tempFile1);
66-
System.out.println("1st document uploaded : \n"+ doc1);
67-
68-
Thread.sleep(1000);
69-
Date since = new Date();
70-
Thread.sleep(1000);
65+
System.out.println(doc1);
7166

7267
File tempFile2 = new File("src/test/resources/document_conversion/html-with-extra-content-input.htm");
7368
Document doc2 = service.uploadDocument(tempFile2);
74-
System.out.println("2nd document uploaded : \n" + doc2);
69+
System.out.println(doc2);
7570

7671
System.out.println("-------------------- Document Collection ------------------------------");
7772
Map<String, Object> docListParams = new HashMap<String, Object>();
78-
docListParams.put(DocumentConversion.LIMIT, 10);
79-
docListParams.put(DocumentConversion.NAME, "html-with-extra-content-input.htm");
80-
docListParams.put(DocumentConversion.SINCE, since);
73+
docListParams.put(DocumentConversion.LIMIT, 2);
8174
docListParams.put(DocumentConversion.MEDIA_TYPE, "text/html");
8275
DocumentCollection documentCollection = service.getDocumentCollection(docListParams);
83-
System.out.println("List of documents with the filtered parameters :\n" + documentCollection);
76+
System.out.println("Document Collection with 2 items in a page :\n" + documentCollection);
8477

8578
// Step 1.1 Get a document
8679
System.out.println("-------------------- Get a document ------------------------------");
@@ -92,16 +85,15 @@ public static void main(String[] args) throws URISyntaxException, IOException, I
9285
List<Property> propertyList = Arrays.asList(
9386
new Property("media_type", "html"), new Property("num_docs", "2"));
9487
Batch batch = service.createBatch("batch_new_name", propertyList);
95-
System.out.println("Create Batch :\n" + batch);
96-
System.out.println("Batch was created successfully with id = " + batch.getId());
88+
System.out.println(batch);
9789

9890
// Step 2. Get a batch
9991
System.out.println("-------------------- Get a batch ------------------------------");
100-
System.out.println("Get Batch :\n" + service.getBatch(batch.getId()));
92+
System.out.println(service.getBatch(batch.getId()));
10193

10294
// Step 2.1 Update a batch
10395
System.out.println("-------------------- Update a batch ------------------------------");
104-
System.out.println("Update Batch :\n" + service.updateBatch(batch.getId(), "batch_NEW_name", null));
96+
System.out.println(service.updateBatch(batch.getId(), "batch_NEW_name", null));
10597

10698
System.out.println("-------------------- Batch Collection ------------------------------");
10799
Map<String, Object> batchListParams = new HashMap<String, Object>();
@@ -112,7 +104,7 @@ public static void main(String[] args) throws URISyntaxException, IOException, I
112104
// Step 3. Add the document to the batch
113105
String batchId2 = batch.getId();
114106
System.out.println("----------------------- Add document to the batch -----------------------");
115-
System.out.println("\n"+ service.addDocumentToBatch(batchId2, doc1.getId()));
107+
System.out.println("Adding documents to the batch :\n"+ service.addDocumentToBatch(batchId2, doc1.getId()));
116108
System.out.println("\n"+ service.addDocumentToBatch(batchId2, doc2.getId()));
117109

118110
System.out.println("-------------------- Batch Document Collection ------------------------------");
@@ -122,12 +114,12 @@ public static void main(String[] args) throws URISyntaxException, IOException, I
122114
// Step 3.1 Get a document from the batch
123115
System.out.println("-------------------- Get a document from batch ------------------------------");
124116
BatchDocumentResponse batchDocumentResponse = service.getBatchDocument(batchId2, doc2.getId());
125-
System.out.println("Get documents from the batch :\n" + batchDocumentResponse);
117+
System.out.println(batchDocumentResponse);
126118

127119
// Step 4. Create a job
128120
System.out.println("-------------------- Create a job ------------------------------");
129121
JobResponse jobResponse = service.createJob("Job 1", batch.getId(), ConversionTarget.ANSWER_UNITS);
130-
System.out.println("Create a job for the batch :\n" + jobResponse);
122+
System.out.println(jobResponse);
131123

132124
// Step 4.1 Create a job with a custom config
133125
JsonObject customJobConfig = new JsonObject();

src/main/java/com/ibm/watson/developer_cloud/document_conversion/v1/DocumentConversion.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,13 @@
5757
*/
5858
public class DocumentConversion extends WatsonService {
5959

60-
/**
61-
* The BATCHES_PATH. (value is "/v1/batches")
62-
**/
63-
public final static String BATCHES_PATH = "/v1/batches";
64-
65-
/**
66-
* The CONVERT_DOCUMENT_PATH. (value is "/v1/convert_document")
67-
**/
68-
public final static String CONVERT_DOCUMENT_PATH = "/v1/convert_document";
69-
7060
/** The default limit for get requests. */
7161
public static final int DEFAULT_LIMIT = 100;
7262

73-
/**
74-
* The DOCUMENTS_PATH. (value is "/v1/documents")
75-
**/
76-
public final static String DOCUMENTS_PATH = "/v1/documents";
77-
7863
/**
7964
* The Constant JOB_ID. (value is "job_id")
8065
*/
8166
public static final String JOB_ID = "job_id";
82-
83-
/**
84-
* The JOBS_PATH. (value is "/v1/jobs")
85-
**/
86-
public final static String JOBS_PATH = "/v1/jobs";
87-
8867
/**
8968
* The Constant LIMIT. (value is "limit")
9069
*/
@@ -100,11 +79,6 @@ public class DocumentConversion extends WatsonService {
10079
*/
10180
public static final String NAME = "name";
10281

103-
/**
104-
* The OUTPUT_PATH. (value is "/v1/output")
105-
**/
106-
public final static String OUTPUT_PATH = "/v1/output";
107-
10882
/**
10983
* The Constant SINCE. (value is "since")
11084
*/

src/main/java/com/ibm/watson/developer_cloud/document_conversion/v1/helpers/BatchDocumentHelper.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Date;
2020
import java.util.Map;
2121

22+
import com.ibm.watson.developer_cloud.util.GsonSingleton;
2223
import org.apache.http.HttpResponse;
2324
import org.apache.http.client.methods.HttpRequestBase;
2425

@@ -68,11 +69,11 @@ public BatchDocumentResponse addDocumentToBatch(final String batchId, final Stri
6869
if (documentId == null || documentId.isEmpty())
6970
throw new IllegalArgumentException("documentId cannot be null or empty");
7071

71-
HttpRequestBase request = Request.Put(DocumentConversion.BATCHES_PATH + "/"+ batchId + "/documents/"+ documentId).build();
72+
HttpRequestBase request = Request.Put(ConfigConstants.BATCHES_PATH + "/"+ batchId + "/documents/"+ documentId).build();
7273
try {
7374
HttpResponse response = docConversionService.execute(request);
7475
String batchDocumentAsJson = ResponseUtil.getString(response);
75-
BatchDocumentResponse batchDocumentResponse = ConversionUtils.getGsonWithIso8601DateDeserializer()
76+
BatchDocumentResponse batchDocumentResponse = GsonSingleton.getGson()
7677
.fromJson(batchDocumentAsJson, BatchDocumentResponse.class);
7778
return batchDocumentResponse;
7879
} catch (IOException e) {
@@ -103,7 +104,7 @@ public BatchDocumentCollection getBatchDocumentCollection(String batchId, Map<St
103104
if (batchId == null || batchId.isEmpty())
104105
throw new IllegalArgumentException("batchId cannot be null or empty");
105106

106-
Request request = Request.Get(DocumentConversion.BATCHES_PATH + "/" + batchId +"/documents");
107+
Request request = Request.Get(ConfigConstants.BATCHES_PATH + "/" + batchId +"/documents");
107108
if(batchDocListParams != null) {
108109
if (batchDocListParams.get(DocumentConversion.TOKEN) != null) {
109110
String token = (String) batchDocListParams.get(DocumentConversion.TOKEN);
@@ -128,8 +129,8 @@ public BatchDocumentCollection getBatchDocumentCollection(String batchId, Map<St
128129
try {
129130
HttpResponse response = docConversionService.execute(requestBase);
130131
String batchDocumentCollectionAsJson = ResponseUtil.getString(response);
131-
BatchDocumentCollection batchDocumentCollection = ConversionUtils.getGsonWithIso8601DateDeserializer()
132-
.fromJson(batchDocumentCollectionAsJson, BatchDocumentCollection.class);
132+
BatchDocumentCollection batchDocumentCollection = GsonSingleton.getGson()
133+
.fromJson(batchDocumentCollectionAsJson, BatchDocumentCollection.class);
133134
return batchDocumentCollection;
134135
} catch (IOException e) {
135136
throw new RuntimeException(e);
@@ -152,11 +153,11 @@ public BatchDocumentResponse getBatchDocument(final String batchId, final String
152153
if (documentId == null || documentId.isEmpty())
153154
throw new IllegalArgumentException("documentId cannot be null or empty");
154155

155-
HttpRequestBase request = Request.Get(DocumentConversion.BATCHES_PATH + "/" + batchId + "/documents/"+ documentId).build();
156+
HttpRequestBase request = Request.Get(ConfigConstants.BATCHES_PATH + "/" + batchId + "/documents/"+ documentId).build();
156157
try {
157158
HttpResponse response = docConversionService.execute(request);
158159
String batchDocumentAsJson = ResponseUtil.getString(response);
159-
BatchDocumentResponse batchDocumentResponse = ConversionUtils.getGsonWithIso8601DateDeserializer()
160+
BatchDocumentResponse batchDocumentResponse = GsonSingleton.getGson()
160161
.fromJson(batchDocumentAsJson, BatchDocumentResponse.class);
161162
return batchDocumentResponse;
162163
} catch (IOException e) {

src/main/java/com/ibm/watson/developer_cloud/document_conversion/v1/helpers/BatchHelper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public Batch createBatch(final String name, final List<Property> properties) {
7575
if(properties != null && !properties.isEmpty())
7676
contentJson.addProperty("properties", new Gson().toJson(properties));
7777

78-
HttpRequestBase request = Request.Post(DocumentConversion.BATCHES_PATH)
78+
HttpRequestBase request = Request.Post(ConfigConstants.BATCHES_PATH)
7979
.withContent(filterJson(contentJson), MediaType.APPLICATION_JSON).build();
8080
try {
8181
HttpResponse response = docConversionService.execute(request);
@@ -105,7 +105,7 @@ public Batch createBatch(final String name, final List<Property> properties) {
105105
* @see DocumentConversion#getBatchCollection(Map)
106106
*/
107107
public BatchCollection getBatchCollection(Map<String, Object> batchListParams) {
108-
Request request = Request.Get(DocumentConversion.BATCHES_PATH);
108+
Request request = Request.Get(ConfigConstants.BATCHES_PATH);
109109

110110
if (batchListParams != null) {
111111
if(batchListParams.get(DocumentConversion.TOKEN) != null){
@@ -157,11 +157,11 @@ public BatchCollection getBatchCollection(Map<String, Object> batchListParams) {
157157
public Batch getBatch(final String batchId) {
158158
if (batchId == null || batchId.isEmpty())
159159
throw new IllegalArgumentException("batchId can not be null or empty");
160-
HttpRequestBase request = Request.Get(DocumentConversion.BATCHES_PATH + "/" + batchId).build();
160+
HttpRequestBase request = Request.Get(ConfigConstants.BATCHES_PATH + "/" + batchId).build();
161161
try {
162162
HttpResponse response = docConversionService.execute(request);
163163
String batchAsJson = ResponseUtil.getString(response);
164-
Batch batch = ConversionUtils.getGsonWithIso8601DateDeserializer().fromJson(batchAsJson, Batch.class);
164+
Batch batch = GsonSingleton.getGson().fromJson(batchAsJson, Batch.class);
165165
return batch;
166166
} catch (IOException e) {
167167
throw new RuntimeException(e);
@@ -189,7 +189,7 @@ public Batch updateBatch(final String batchId, final String name,
189189
contentJson.addProperty("name", name);
190190
if(properties != null && properties.isEmpty())
191191
contentJson.addProperty("properties", new Gson().toJson(properties));
192-
HttpRequestBase request = Request.Put(DocumentConversion.BATCHES_PATH + "/" + batchId)
192+
HttpRequestBase request = Request.Put(ConfigConstants.BATCHES_PATH + "/" + batchId)
193193
.withContent(filterJson(contentJson),
194194
MediaType.APPLICATION_JSON).build();
195195
try {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.ibm.watson.developer_cloud.document_conversion.v1.helpers;
2+
3+
import com.ibm.watson.developer_cloud.document_conversion.v1.DocumentConversion;
4+
5+
/**
6+
* The constants class for DocumentConversion service helpers
7+
*
8+
* @see DocumentConversion
9+
*/
10+
class ConfigConstants {
11+
12+
/**
13+
* The BATCHES_PATH. (value is "/v1/batches")
14+
**/
15+
static final String BATCHES_PATH = "/v1/batches";
16+
17+
/**
18+
* The CONVERT_DOCUMENT_PATH. (value is "/v1/convert_document")
19+
**/
20+
static final String CONVERT_DOCUMENT_PATH = "/v1/convert_document";
21+
22+
/**
23+
* The DOCUMENTS_PATH. (value is "/v1/documents")
24+
**/
25+
static final String DOCUMENTS_PATH = "/v1/documents";
26+
27+
/**
28+
* The JOBS_PATH. (value is "/v1/jobs")
29+
**/
30+
static final String JOBS_PATH = "/v1/jobs";
31+
32+
/**
33+
* The OUTPUT_PATH. (value is "/v1/output")
34+
**/
35+
static final String OUTPUT_PATH = "/v1/output";
36+
37+
}

src/main/java/com/ibm/watson/developer_cloud/document_conversion/v1/helpers/ConversionUtils.java

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
* @see DocumentConversion
3939
*/
4040
public class ConversionUtils {
41+
42+
/** The Constant DATE_FORMAT_UTC. */
43+
private static final String DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
4144

4245
/**
4346
* Returns the media type for a given file.
@@ -92,7 +95,7 @@ public static Boolean isValidMediaType(final String mediaType) {
9295
*/
9396
public static String convertToISO(Date date) {
9497
TimeZone tz = TimeZone.getTimeZone("UTC");
95-
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
98+
DateFormat df = new SimpleDateFormat(DATE_FORMAT_UTC);
9699
df.setTimeZone(tz);
97100
String dtAsISO = df.format(date);
98101
return dtAsISO;
@@ -108,39 +111,4 @@ public static String writeInputStreamToString(InputStream is) {
108111
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
109112
return s.hasNext() ? s.next() : "";
110113
}
111-
112-
/**
113-
* Gson singleton with an ISO 8601 Date Deserializer.
114-
*
115-
* @return Gson
116-
*/
117-
public static Gson getGsonWithIso8601DateDeserializer() {
118-
if (gsonWithIso8601DateDeserializer == null) {
119-
gsonWithIso8601DateDeserializer = new GsonBuilder()
120-
.registerTypeAdapter(Date.class, iso8601DateDeserializer).create();
121-
}
122-
return gsonWithIso8601DateDeserializer;
123-
}
124-
125-
/** Gson singleton with an ISO 8601 Date Deserializer. */
126-
private static Gson gsonWithIso8601DateDeserializer;
127-
128-
/** JSON deserializer for ISO 8601 dates into Java Date objects. */
129-
private static JsonDeserializer<Date> iso8601DateDeserializer = new JsonDeserializer<Date>() {
130-
@Override
131-
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
132-
throws JsonParseException {
133-
if (json == null) {
134-
return null;
135-
}
136-
String date = json.getAsString();
137-
SimpleDateFormat iso8601DateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
138-
try {
139-
return iso8601DateTimeFormat.parse(date);
140-
} catch (Exception e) {
141-
throw new JsonParseException("Unable to parse date " + date, e);
142-
}
143-
}
144-
};
145-
146114
}

src/main/java/com/ibm/watson/developer_cloud/document_conversion/v1/helpers/ConvertDocumentHelper.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.InputStream;
2121
import java.nio.charset.Charset;
2222

23+
import com.ibm.watson.developer_cloud.util.GsonSingleton;
2324
import org.apache.http.HttpResponse;
2425
import org.apache.http.client.methods.HttpRequestBase;
2526
import org.apache.http.entity.mime.MultipartEntity;
@@ -68,8 +69,8 @@ public ConvertDocumentHelper(DocumentConversion docConversionService) {
6869
public Answers convertDocumentToAnswer(final File document) {
6970
InputStream is = convertDocument(document, ConversionTarget.ANSWER_UNITS);
7071
String convertedDocument = ConversionUtils.writeInputStreamToString(is);
71-
Answers answers = ConversionUtils.getGsonWithIso8601DateDeserializer()
72-
.fromJson(convertedDocument, Answers.class);
72+
Answers answers = GsonSingleton.getGson().fromJson(
73+
convertedDocument, Answers.class);
7374
return answers;
7475
}
7576

@@ -115,7 +116,7 @@ public InputStream convertDocument(final File document, String mediaType, final
115116
String json = configRequestJson.toString();
116117
reqEntity.addPart("config", new StringBody(json, MediaType.APPLICATION_JSON, Charset.forName("UTF-8")));
117118

118-
HttpRequestBase request = Request.Post(DocumentConversion.CONVERT_DOCUMENT_PATH)
119+
HttpRequestBase request = Request.Post(ConfigConstants.CONVERT_DOCUMENT_PATH)
119120
.withEntity(reqEntity).build();
120121

121122
HttpResponse response = docConversionService.execute(request);
@@ -136,8 +137,8 @@ public InputStream convertDocument(final File document, String mediaType, final
136137
public Answers convertDocumentToAnswer(final String documentId) {
137138
InputStream is = convertDocument(documentId, ConversionTarget.ANSWER_UNITS);
138139
String convertedDocument = ConversionUtils.writeInputStreamToString(is);
139-
Answers answers = ConversionUtils.getGsonWithIso8601DateDeserializer()
140-
.fromJson(convertedDocument, Answers.class);
140+
Answers answers = GsonSingleton.getGson().fromJson(
141+
convertedDocument, Answers.class);
141142
return answers;
142143
}
143144

@@ -160,7 +161,7 @@ public InputStream convertDocument(final String documentId, final ConversionTarg
160161
contentJson.addProperty("document_id", documentId);
161162
contentJson.addProperty("conversion_target", conversionTarget.toString());
162163

163-
HttpRequestBase request = Request.Post(DocumentConversion.CONVERT_DOCUMENT_PATH)
164+
HttpRequestBase request = Request.Post(ConfigConstants.CONVERT_DOCUMENT_PATH)
164165
.withContent(contentJson).build();
165166

166167
try {

0 commit comments

Comments
 (0)