I have a client (java) that send some json data to my server (c++). After this my server respond with some informations depending the operation made my java client. For now its works.
Example body request:
{
"userEmail": "email@email.com",
"userPassword": "12345678"
}
And the server, receive the email and password and do the operations and send back a response.
But now i need to change my java client to send the request like this:
{
"userInformation":{
"userEmail": "email@email.com",
"userPassword": "12345678"
}
}
This request can be use to do the login. This is a very complex architecture so i cannot copy all code, but in login class i used the gson (Note i only work in c++ server, i dont work in client and its impossible to contact the guy that made the java client to ask him about this doubts)
Java client - login class
public final String userEmail;
public final String userPassword;
public LoginRequestArgs( String userEmail, String userPassword)
{
this.userEmail = userEmail;
this.userPassword = userPassword;
}
public static LoginRequestArgs fromStringJson(String data)
{
try
{
Gson gson = new Gson();
return gson.fromJson(data, LoginRequestArgs.class);
}
catch(Exception e)
{
return null;
}
}
public static LoginRequestArgs fromBytesJson(byte[] data)
{
if (data == null) return null;
try
{
String str = new String(data, "utf-8");
return fromStringJson(str);
}
catch (Exception e)
{
return null;
}
}
@Override
public String toJsonString()
{
try
{
Gson gson = new Gson();
return gson.toJson(this);
}
catch(Exception e)
{
return null;
}
}
@Override
public byte[] toJsonBytes()
{
try
{
return this.toJsonString().getBytes("utf-8");
}
catch (Exception e)
{
return null;
}
}
I have other class that connect to my server and send the request. This class call the login function (the request is a SerializableJSON variable) and place this request to my server request = LoginRequestArgs.fromStringJson(args.httpEntity);
(for tests i use one rest client chrome extension)
I know that can be some hard to understand but its not easy to explain all things. I try to explain the essencial.
real problem: i cannot adapt the java client to send the email and password that are inside "userInformation". Someone can help me please? Thanks
EDIT (OTHER BODY REQUEST EXAMPLE):
{
"userInformation": {
"otherSection": {
"key1": "value1",
"key2": "value2"
}
}
}
EDIT 2 (authentication method):
{
"authenticationMethod": {
"sessionId": {
"email": "email@email.com",
"password": "pass123"
}
},
"userInformation": {
"userId": "user",
"userPassword": "user123"
}
}
EDIT 3 (TOKEN):
{
"authenticationMethod": {
"token": {
"token": "HDI393UDDNDMAY4758SAD"
}
},
"userInformation": {
"userId": "user",
"userPassword": "user123"
}
}
EDIT 4:
{
"sessionId":{
"email":"email@email.com",
"password":"dasdas"
},
"userInformation":{
"userId": "userId1",
"userPassword": "12345678"
}
}
I already send this json to my c++ server and its work and my decode session id working too, its not problem for me work on c++. But i need to send sessionId (or token) but inside "AuthenticationMethod", its only the thing i need to implement now. Note the "userInformation" its like and example it can be for example "bookInformation", "carInformation" , depending the request type, i send different data with different keys/values inside.. but the authentication method (session id or token) it is always mandatory to use in all request.
To work like i show to you i implement this:
public class SessionId {
public String email;
public String password;
public SessionId(String email, String password)
{
this.email = email;
this.password = password;
}
}
And inside the construct of my class (can be for example one class like LoginRequestArgs, the login cass) i call the super:
public UserInfo userInformation;
public SessionId sessionId;
public LoginRequestArgs(String email, String password,String userEmail, String userPassword)
{
super(email,password);
userInformation = new UserInfo(userEmail, userPassword);
}
static class UserInfo {
public String userId;
public String userPassword;
public UserInfo(String userEmail, String userPassword) {
this.userId = userEmail;
this.userPassword = userPassword;
}
}
So for now, i only need to add "authenticationMethod" before session id or token (i believe the way to do this its the same for both)
**** EDIT 5 ********
login.java
public class LoginRequestArgs implements SerializableJSON {
public UserInfo userInformation;
public AuthenticationMethod authenticationMethod;
public LoginRequestArgs(String email, String password,String userId, String userPassword)
{
AuthenticationMethod auth = new SessionId(email, password);
setAuth(auth);
userInformation = new UserInfo(userId, userPassword);
}
public void setAuth(AuthenticationMethod authenticationMethod){
this.authenticationMethod = authenticationMethod;
}
static class UserInfo {
public String userId;
public String userPassword;
public UserInfo(String userEmail, String userPassword) {
this.userId = userEmail;
this.userPassword = userPassword;
}
}
SessionId.java
public class SessionId extends AuthenticationMethod {
Session sessionId;
public SessionId(String email, String password)
{
this.sessionId = new Session(email,password);
}
static class Session{
String email;
String password;
public Session(String email, String password)
{
this.email = email;
this.password = password;
}
}
}
AuthenticationMethod.java
public class AuthenticationMethod {
}