io.crnk.rs.JaxrsRequestContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of crnk-rs Show documentation
Show all versions of crnk-rs Show documentation
JSON API framework for Java
package io.crnk.rs;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.crnk.core.engine.http.HttpRequestContextBase;
import io.crnk.core.engine.internal.utils.UrlUtils;
import io.crnk.core.utils.Nullable;
import io.crnk.legacy.internal.RepositoryMethodParameterProvider;
import io.crnk.rs.internal.legacy.JaxrsParameterProvider;
public class JaxrsRequestContext implements HttpRequestContextBase {
private final CrnkFeature feature;
private ContainerRequestContext requestContext;
private String path;
private Map> parameters;
private Map responseHeaders = new HashMap<>();
private Nullable requestBody = Nullable.empty();
private Integer responseCode;
private byte[] responseBody;
private RepositoryMethodParameterProvider requestParameterProvider;
JaxrsRequestContext(ContainerRequestContext requestContext, CrnkFeature feature) {
this.feature = feature;
this.requestContext = requestContext;
UriInfo uriInfo = requestContext.getUriInfo();
this.path = buildPath(uriInfo);
this.parameters = getParameters(uriInfo);
ObjectMapper objectMapper = feature.getBoot().getObjectMapper();
requestParameterProvider =
new JaxrsParameterProvider(objectMapper, requestContext, feature.getParameterProviderRegistry());
}
@Override
public String getResponseHeader(String name) {
return responseHeaders.get(name);
}
@Override
public RepositoryMethodParameterProvider getRequestParameterProvider() {
return requestParameterProvider;
}
@Override
public String getRequestHeader(String name) {
return requestContext.getHeaderString(name);
}
@Override
public Map> getRequestParameters() {
return parameters;
}
@Override
public String getPath() {
return path;
}
@Override
public String getBaseUrl() {
return UrlUtils.removeTrailingSlash(requestContext.getUriInfo().getBaseUri().toString());
}
@Override
public byte[] getRequestBody() throws IOException {
if (!requestBody.isPresent()) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
InputStream is = requestContext.getEntityStream();
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
requestBody = Nullable.of(buffer.toByteArray());
}
return requestBody.get();
}
@Override
public void setResponseHeader(String name, String value) {
this.responseHeaders.put(name, value);
}
@Override
public void setResponse(int code, byte[] body) {
this.responseCode = code;
this.responseBody = body;
}
@Override
public String getMethod() {
return requestContext.getMethod();
}
public void checkAbort() {
if (responseCode != null) {
Response.ResponseBuilder builder = Response.status(responseCode);
if (responseBody != null) {
builder = builder.entity(new ByteArrayInputStream(responseBody));
}
for (Map.Entry entry : responseHeaders.entrySet()) {
builder.header(entry.getKey(), entry.getValue());
}
requestContext.abortWith(builder.build());
}
}
private Map> getParameters(UriInfo uriInfo) {
MultivaluedMap queryParametersMultiMap = uriInfo.getQueryParameters();
Map> queryParameters = new HashMap<>();
for (Map.Entry> queryEntry : queryParametersMultiMap.entrySet()) {
queryParameters.put(queryEntry.getKey(), new LinkedHashSet<>(queryEntry.getValue()));
}
return queryParameters;
}
private String buildPath(UriInfo uriInfo) {
String basePath = uriInfo.getPath();
String webPathPrefix = feature.getWebPathPrefix();
if (webPathPrefix != null && basePath.startsWith(webPathPrefix)) {
return basePath.substring(webPathPrefix.length());
}
else {
return basePath;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy