Skip to content

Commit

Permalink
AuthZEN - added simple request/response classes for client
Browse files Browse the repository at this point in the history
Signed-off-by: YuriyZ <[email protected]>
  • Loading branch information
yuriyz committed Nov 11, 2024
1 parent a49f378 commit 24a596f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.jans.as.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jans.as.model.util.QueryBuilder;
import io.jans.model.authzen.AccessEvaluationRequest;
import org.apache.log4j.Logger;

/**
* @author Yuriy Z
*/
public class AccessEvaluationClientRequest extends ClientAuthnRequest implements IsJsonRequest {

private static final Logger LOG = Logger.getLogger(AccessEvaluationClientRequest.class);
private static final ObjectMapper MAPPER = new ObjectMapper();

private AccessEvaluationRequest request;

public AccessEvaluationClientRequest() {
setContentType("application/json");
}

public AccessEvaluationRequest getRequest() {
return request;
}

public AccessEvaluationClientRequest setRequest(AccessEvaluationRequest request) {
this.request = request;
return this;
}

@Override
public String getQueryString() {
QueryBuilder builder = QueryBuilder.instance();
if (request == null) {
return builder.toString();
}

appendClientAuthnToQuery(builder);
for (String key : getCustomParameters().keySet()) {
builder.append(key, getCustomParameters().get(key));
}

return builder.toString();
}

@Override
public String toString() {
return "AccessEvaluationClientRequest{" +
"request=" + request +
"} " + super.toString();
}

@Override
public String asJson() {
try {
return request != null ? MAPPER.writeValueAsString(request) : "";
} catch (JsonProcessingException e) {
LOG.error("Failed to serialize request", e);
return "";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.jans.as.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jans.model.authzen.AccessEvaluationResponse;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
* @author Yuriy Z
*/
public class AccessEvaluationClientResponse extends BaseResponse {

private static final Logger LOG = Logger.getLogger(AccessEvaluationClientResponse.class);
private static final ObjectMapper MAPPER = new ObjectMapper();

private AccessEvaluationResponse response;

public AccessEvaluationClientResponse(Response clientResponse) {
super(clientResponse);
injectDataFromJson(entity);
}

public void injectDataFromJson(String json) {
if (StringUtils.isBlank(json)) {
return;
}

try {
response = MAPPER.readValue(json, AccessEvaluationResponse.class);
} catch (JsonProcessingException e) {
LOG.error("Failed to read json: " + json, e);
}
}

public AccessEvaluationResponse getResponse() {
return response;
}

public AccessEvaluationClientResponse setResponse(AccessEvaluationResponse response) {
this.response = response;
return this;
}

@Override
public String toString() {
return "AccessEvaluationClientResponse{" +
"response=" + response +
"} " + super.toString();
}
}

0 comments on commit 24a596f

Please sign in to comment.