0
votes

I don't really see where is my mistake in my java code. I have to log in to Kofax Total Agility using REST API. For this I tried to use postman to test if my json was correctly built. Here is my login JSON :

{
  "userIdentityWithPassword": {
    "LogOnProtocol": "7",
    "UnconditionalLogOn": false,
    "UserId": "myLogin",
    "Password": "myPassword"
  }
}

I obtain a positive answer :

{
    "d": {
        "__type": "Session2:http://www.kofax.com/agility/services/sdk",
        "SessionId": "1DE6B79F34054D58AEE1509FE583811F",
        "ResourceId": "873C0F5C8BD34BAFBF4B14FF538FBAEC",
        "DisplayName": "Aurore Mouret",
        "IsValid": true,
        "LogonState": {
            "__type": "LogonState:http://www.kofax.com/agility/services/sdk",
            "FormName": "",
            "LogonStateType": 0
        },
        "ReserveLicenseUsed": false
    }
}

So far, so good. For this I created models :

public class UserIdentityWithPasswordRestRequestModel {
    LogOnWithPassword2RestRequestModel userIdentityWithPassword;
}
public class LogOnWithPassword2RestRequestModel {
    @SerializedName("LogOnProtocol")
    private String logOnProtocol;
    @SerializedName("UnconditionalLogOn")
    private boolean unconditionalLogOn;
    @SerializedName("UserId")
    private String userId; // C640521793431F4486D4EF1586672385
    @SerializedName("Password")
    private String password; // 123456
}

For the response :

public class LogOnWithPassword2RestResponseModel {
    private DRestResponseModel d;
}
public class DRestResponseModel {
    @SerializedName("__type")
    private String type;
    @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;
}
public class LogonStateRestResponseModel {
    @SerializedName("__type")
    private String type;
    @SerializedName("FormName")
    private String formName;
    @SerializedName("LogonStateType")
    private String logonStateType;
}

Those classes should allow me to build the json. Now I created a method that build the request object and expect a reponse object.

public LogOnWithPassword2RestResponseModel logOnWithPassword() throws Exception {
    LogOnWithPassword2RestResponseModel returnValue = new LogOnWithPassword2RestResponseModel();

    // set the HTTP Connection to the KTA Application
    URL url = new URL("http://localhost/TotalAgility/Services/SDK/UserService.svc/json/LogOnWithPassword2");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();

    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json; utf-8");
    con.setDoOutput(true);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    LogOnWithPassword2RestRequestModel userIdentityWithPassword = new LogOnWithPassword2RestRequestModel();
    // set the values
    userIdentityWithPassword.setLogOnProtocol(logOnProtocol);
    userIdentityWithPassword.setUnconditionalLogOn(unconditionalLogOn);
    userIdentityWithPassword.setUserId(userId);
    userIdentityWithPassword.setPassword(password);

    UserIdentityWithPasswordRestRequestModel userIdentityWithPasswordRestRequestModel =
            new UserIdentityWithPasswordRestRequestModel();
    userIdentityWithPasswordRestRequestModel.setUserIdentityWithPassword(userIdentityWithPassword);

    // Convert to Json :
    String jsonInputString = gson.toJson(userIdentityWithPasswordRestRequestModel);
    System.out.println(jsonInputString);

    // add request parameter, form parameters
    try(OutputStream os = con.getOutputStream()) {
        byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
        os.write(input, 0, input.length);
        System.out.println("OS " + os);
    }

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

    return returnValue;
}

When i call this part of code, I note that I build the "right" JSON :

{
  "userIdentityWithPassword": {
    "LogOnProtocol": "7",
    "UnconditionalLogOn": false,
    "UserId": "myLogin",
    "Password": "myPassword"
  }
}

For a reason I can't explain, I obtain an error 400.

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://94.247.28.163/TotalAgility/Services/SDK/UserService.svc/json/LogOnWithPassword2
    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.logOnWithPassword(DocumentController.java:71)
    at com.signature.app.Main.main(Main.java:21)

The line 71 is corresponding to this line of the try catch

try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)))

1
I am not a java expert but I think you are probably missing some header like Content-Type:'application/json' in the REST request. - Prabhjot Singh Kainth
I add the content type here : con.setRequestProperty("Content-Type", "application/json; utf-8"); - davidvera
Does it works successfully on POSTMAN? - Prabhjot Singh Kainth
Yes ! I finally solved it - davidvera

1 Answers

1
votes

I replaced

con.setRequestProperty("Content-Type", "application/json; utf-8");

With this code :

con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept", "application/json");