use of jakarta.ws.rs.core.MultivaluedMap in project mycore by MyCoRe-Org.
the class MCRRestAuthorizationFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) {
final String method = requestContext.getMethod();
if (HttpMethod.OPTIONS.equals(method)) {
return;
}
final MCRRestRequiredPermission annotation = resourceInfo.getResourceMethod().getAnnotation(MCRRestRequiredPermission.class);
final MCRRestAPIACLPermission permission = Optional.ofNullable(annotation).map(a -> a.value()).orElseGet(() -> MCRRestAPIACLPermission.fromMethod(method));
Optional.ofNullable(resourceInfo.getResourceClass().getAnnotation(Path.class)).map(Path::value).ifPresent(path -> {
checkRestAPIAccess(requestContext, permission, path);
MultivaluedMap<String, String> pathParameters = requestContext.getUriInfo().getPathParameters();
checkBaseAccess(requestContext, permission, pathParameters.getFirst(PARAM_MCRID), pathParameters.getFirst(PARAM_DERID), pathParameters.getFirst(PARAM_DER_PATH));
});
checkDetailLevel(requestContext, requestContext.getAcceptableMediaTypes().stream().map(m -> m.getParameters().get(MCRDetailLevel.MEDIA_TYPE_PARAMETER)).filter(Objects::nonNull).toArray(String[]::new));
}
use of jakarta.ws.rs.core.MultivaluedMap in project mycore by MyCoRe-Org.
the class MCRRestAuthorizationFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
MCRRestAPIACLPermission permission;
switch(requestContext.getMethod()) {
case HttpMethod.OPTIONS:
return;
case HttpMethod.GET:
case HttpMethod.HEAD:
permission = MCRRestAPIACLPermission.READ;
break;
case HttpMethod.DELETE:
permission = MCRRestAPIACLPermission.DELETE;
break;
default:
permission = MCRRestAPIACLPermission.WRITE;
}
Optional.ofNullable(resourceInfo.getResourceClass().getAnnotation(Path.class)).map(Path::value).ifPresent(path -> {
try {
checkRestAPIAccess(requestContext, permission, path);
MultivaluedMap<String, String> pathParameters = requestContext.getUriInfo().getPathParameters();
checkBaseAccess(requestContext, permission, pathParameters.getFirst(PARAM_MCRID), pathParameters.getFirst(PARAM_DERID), pathParameters.getFirst(PARAM_DER_PATH));
} catch (MCRRestAPIException e) {
LogManager.getLogger().warn("API Access denied!");
requestContext.abortWith(new MCRRestAPIExceptionMapper().toResponse(e));
}
});
try {
checkDetailLevel(requestContext, requestContext.getAcceptableMediaTypes().stream().map(m -> m.getParameters().get(MCRDetailLevel.MEDIA_TYPE_PARAMETER)).filter(Objects::nonNull).toArray(String[]::new));
} catch (MCRRestAPIException e) {
LogManager.getLogger().warn("API Access denied!");
requestContext.abortWith(new MCRRestAPIExceptionMapper().toResponse(e));
}
}
use of jakarta.ws.rs.core.MultivaluedMap in project mycore by MyCoRe-Org.
the class MCRPropertiesToJSONTransformer method writeTo.
@Override
public void writeTo(Properties t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
httpHeaders.putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_TYPE.withCharset(StandardCharsets.UTF_8.name()));
final Type mapType = new TypeToken<Map<String, String>>() {
private static final long serialVersionUID = 1L;
}.getType();
Map<String, String> writeMap = t.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));
String json = new GsonBuilder().registerTypeAdapter(mapType, new BundleMapSerializer()).create().toJson(writeMap, mapType);
entityStream.write(json.getBytes(StandardCharsets.UTF_8));
}
use of jakarta.ws.rs.core.MultivaluedMap in project mycore by MyCoRe-Org.
the class MCRWrappedXMLWriter method writeTo.
@Override
public void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
Collection collection = (type.isArray()) ? Arrays.asList((Object[]) t) : (Collection) t;
Class elementType = getElementClass(type, genericType);
Supplier<Marshaller> m = () -> {
try {
JAXBContext ctx = CTX_MAP.computeIfAbsent(elementType, et -> {
try {
return JAXBContext.newInstance(et);
} catch (JAXBException e) {
throw new InternalServerErrorException(e);
}
});
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
return marshaller;
} catch (JAXBException e) {
throw new InternalServerErrorException(e);
}
};
try {
XmlElementWrapper wrapper = Stream.of(annotations).filter(a -> XmlElementWrapper.class.isAssignableFrom(a.annotationType())).map(XmlElementWrapper.class::cast).findAny().get();
writeCollection(wrapper, collection, StandardCharsets.UTF_8, m, entityStream);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
use of jakarta.ws.rs.core.MultivaluedMap in project tomee by apache.
the class InjectionUtils method processValues.
private static List<MultivaluedMap<String, String>> processValues(Class<?> type, Type genericType, MultivaluedMap<String, String> values, boolean isbean) {
final List<MultivaluedMap<String, String>> valuesList;
if (isbean && InjectionUtils.isSupportedCollectionOrArray(type)) {
valuesList = new ArrayList<>();
Class<?> realType = InjectionUtils.getActualType(genericType);
for (Map.Entry<String, List<String>> entry : values.entrySet()) {
String memberKey = entry.getKey();
Class<?> memberType = null;
for (Method m : realType.getMethods()) {
if (m.getName().equalsIgnoreCase("set" + memberKey) && m.getParameterTypes().length == 1) {
memberType = m.getParameterTypes()[0];
break;
}
}
if (memberType == null) {
for (Field f : realType.getFields()) {
if (f.getName().equalsIgnoreCase(memberKey)) {
memberType = f.getType();
break;
}
}
}
// {c=[71, 81, 91, 72, 82, 92], a=[C1, C2], b=[790, 791]}
if (memberType != null && InjectionUtils.isSupportedCollectionOrArray(memberType)) {
continue;
}
// Split multivaluedmap value list contents into separate multivaluedmap instances
// whose list contents are only 1 level deep, for example:
// {a=[C1, C2], b=[790, 791]}
// becomes these 2 separate multivaluedmap instances:
// {a=[C1], b=[790]} and {a=[C2], b=[791]}
int idx = 0;
for (String value : entry.getValue()) {
MultivaluedMap<String, String> splitValues = (idx < valuesList.size()) ? valuesList.get(idx) : null;
if (splitValues == null) {
splitValues = new MetadataMap<>();
valuesList.add(splitValues);
}
splitValues.add(memberKey, value);
idx++;
}
}
} else {
valuesList = Collections.singletonList(values);
}
return valuesList;
}
Aggregations