KTA用Rest API Java替换文档

时间:2019-12-23 11:45:39

标签: java rest kofax

我找到了一种允许从KTA检索文件的方法。该方法签名如下:

DocumentSourceFile GetSourceFile(string sessionId, ReportingData reportingData, string  documentId) 

由于必须使用Java进行呼叫,因此我为此对象创建了一个模型:

@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class GetSourceFileRestRequestModel {
    private String sessionId;
    private String documentId;
}

我还为响应创建了一个模型:

@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class DRestResponseModel {
    // common to all
    @SerializedName("__type")
    private String type;

    // for login
    @SerializedName("SessionId")
    private String sessionId;
    @SerializedName("ResourceId")
    private String resourceId;
    @SerializedName("DisplayName")
    private String displayName;
    @SerializedName("IsValid")
    private boolean isValid;
    @SerializedName("LogonState")
    private LogonStateRestResponseModel logonState;
    @SerializedName("ReserveLicenseUsed")
    private boolean reserveLicenseUsed;

    // for GettingSourceFile
    @SerializedName("MimeType")
    private String mimeType;
    @SerializedName("SourceFile")
    private byte[] sourceFile;
}

我确实在本地成功检索了我的文件。我还发现了一种允许在KTA上更新文档源文件的方法。此方法具有此签名

void UpdateSourceFile   (string     sessionId, ReportingData    reportingData, string   documentId, DocumentSourceFile  sourceFile)     

我遵循与以前相同的协议...所以我创建了模型:

@Component
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class UpdateSourceFileRequestModel {

    private String sessionId;

    private String documentId;

    @SerializedName("SourceFile")
    private int[] sourceFile;
}

由于reportingData为null,因此我不必将其放入模型中。到目前为止,一切都很好。

在控制器中,我创建了一个简单的方法来获取文件,将其转换为int数组(无符号字节数组),然后将其作为json发送回KTA应用程序。

private void updateSourceFile(String fileToReturn, LogOnWithPassword2RestResponseModel login) throws IOException {
    System.out.println("\nSending HTTP POST request - returning file process");
    // retrieve the file - for testing purpose i have a second file 
    Path path = Paths.get(fileToReturn);
    File f = path.toFile();
    // convert file to byte array
    byte[] array = Files.readAllBytes(Paths.get(fileToReturn));
    int[] result = new int[array.length];
    for(int i = 0; i < array.length; i++)   {
        result[i] = unsignedToBytes((byte) (array[i] - 12));
    }

    UpdateSourceFileRequestModel updateSourceFileRequestModel = new UpdateSourceFileRequestModel();
    updateSourceFileRequestModel.setSessionId(login.getD().getSessionId());
    updateSourceFileRequestModel.setDocumentId(docId);
    updateSourceFileRequestModel.setSourceFile(result);

    String url = "http://localhost/TotalAgility/Services/SDK/CaptureDocumentService.svc/json/UpdateSourceFile";
    HttpURLConnection con = postApi(url);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    String jsonInputString = gson.toJson(updateSourceFileRequestModel);
    //System.out.println("updateSourceFileRequestModel = " + jsonInputString);

    // return the file
    try(OutputStream os = con.getOutputStream()) {
        byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
        os.write(input, 0, input.length);
        System.out.println(os);
    }

    // get the response from KTA
    try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
        StringBuilder response = new StringBuilder();
        String responseLine;
        while ((responseLine = br.readLine()) != null) {
            response.append(responseLine.trim());
        }
        System.out.println(response.toString());
        updateSourceFileRequestModel = gson.fromJson(response.toString(), UpdateSourceFileRequestModel.class);
        System.out.println(updateSourceFileRequestModel);
    }

}

private int unsignedToBytes(byte b) {
    return b & 0xFF;
}

// set the connection
private HttpURLConnection postApi (String apiUrl) throws IOException {
    URL url = new URL(apiUrl);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestProperty("Accept", "application/json");
    conn.setDoOutput(true);

    return conn;
}

我收到一条错误消息:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost/TotalAgility/Services/SDK/CaptureDocumentService.svc/json/UpdateSourceFile
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
    at com.signature.app.ui.controller.DocumentController.updateSourceFile(DocumentController.java:236)
    at com.signature.app.ui.controller.DocumentController.GetSourceFile(DocumentController.java:197)
    at com.signature.app.ui.controller.DocumentController.logOnWithPassword(DocumentController.java:88)
    at com.signature.app.Main.main(Main.java:18)

当我尝试在邮递员上使用邮寄请求时,出现错误500,并显示以下消息:“发生了非故障异常” 我还尝试了其他HTTP协议,例如get,put或patch。在这些情况下,我会出现405错误。至少我知道我的POST请求是我必须使用的请求。

我试图用以下内容替换邮政编码:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String jsonInputString = gson.toJson(updateSourceFileRequestModel);
StringEntity postingString = new StringEntity(jsonInputString);
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
post.setHeader("accept", "application/json");
HttpResponse response = httpClient.execute(post);
System.out.println(response);

现在我得到了:

HttpResponseProxy{HTTP/1.1 500 Unknown Exception [Cache-Control: private, Content-Type: application/xml; charset=utf-8, X-Frame-Options: SAMEORIGIN, Date: Mon, 23 Dec 2019 12:27:28 GMT, Content-Length: 39] ResponseEntityProxy{[Content-Type: application/xml; charset=utf-8,Content-Length: 39,Chunked: false]}}

0 个答案:

没有答案