private static Map<String, MediaType> extractMediaTypeMappings(final Object mappings) { // parse and validate mediaTypeMappings set through MEDIA_TYPE_MAPPINGS property return parseAndValidateMappings(ServerProperties.MEDIA_TYPE_MAPPINGS, mappings, new TypeParser<MediaType>() { public MediaType valueOf(final String value) { return MediaType.valueOf(value); } }); }
/** * Initializes this filter setting the sources of information the filter should look for. * * @param sources Sources of method override information. If empty, both * {@link org.glassfish.jersey.server.filter.HttpMethodOverrideFilter.Source#HEADER} and * {@link org.glassfish.jersey.server.filter.HttpMethodOverrideFilter.Source#QUERY} will * be added to the config by default. */ public HttpMethodOverrideFilter(final Source... sources) { int c = 0; for (final Source cf : sources) { if (cf != null) { c |= cf.getFlag(); } } if (c == 0) { c = 3; } this.config = c; }
/** * Returns parameter value in a normalized form (uppercase, trimmed and {@code null} if empty string) * considering the config flags. * * @param source Config flag to look for (if set in the config, this method returns the param value, * if not set, this method returns {@code null}). * @param paramsMap Map to retrieve the parameter from. * @param paramName Name of the parameter to retrieve. * @return Normalized parameter value. Never returns an empty string - converts it to {@code null}. */ private String getParamValue(final Source source, final MultivaluedMap<String, String> paramsMap, final String paramName) { String value = source.isPresentIn(config) ? paramsMap.getFirst(paramName) : null; if (value == null) { return null; } value = value.trim(); return value.length() == 0 ? null : value.toUpperCase(); }
/** * Create a filter that reads the configuration (media type and language mappings) * from the provided {@link ResourceConfig} instance. * This constructor will be called by the Jersey runtime when the filter * class is returned from {@link javax.ws.rs.core.Application#getClasses()}. * The {@link ResourceConfig} instance will get auto-injected. * * @param rc ResourceConfig instance that holds the configuration for the filter. */ public UriConnegFilter(@Context final Configuration rc) { this(extractMediaTypeMappings(rc.getProperty(ServerProperties.MEDIA_TYPE_MAPPINGS)), extractLanguageMappings(rc.getProperty(ServerProperties.LANGUAGE_MAPPINGS))); }
private static <T> Map<String, T> parseAndValidateMappings(final String property, final Object mappings, final TypeParser<T> parser) { if (mappings == null) { return Collections.emptyMap(); } if (mappings instanceof Map) { return (Map<String, T>) mappings; } final HashMap<String, T> mappingsMap = new HashMap<>(); if (mappings instanceof String) { parseMappings(property, (String) mappings, mappingsMap, parser); } else if (mappings instanceof String[]) { final String[] mappingsArray = (String[]) mappings; for (final String aMappingsArray : mappingsArray) { parseMappings(property, aMappingsArray, mappingsMap, parser); } } else { throw new IllegalArgumentException(LocalizationMessages.INVALID_MAPPING_TYPE(property)); } encodeKeys(mappingsMap); return mappingsMap; }
public static ContentEncoding fromString(String input) throws ParseException { HttpHeaderReader reader = HttpHeaderReader.newInstance(input); // Skip any white space reader.hasNext(); return new ContentEncoding(reader.nextToken().toString(), HttpHeaderReader.readQualityFactorParameter(reader)); }
/** * Create a filter that reads the configuration ({@link ServerProperties#HTTP_METHOD_OVERRIDE}) * from the provided {@link org.glassfish.jersey.server.ResourceConfig} instance. * <p/> * This constructor will be called by the Jersey runtime when the filter class is returned from * {@link javax.ws.rs.core.Application#getClasses()}. The {@link org.glassfish.jersey.server.ResourceConfig} * instance will get auto-injected. * * @param rc ResourceConfig instance that holds the configuration for the filter. */ public HttpMethodOverrideFilter(@Context final Configuration rc) { this(parseConfig(rc.getProperty(ServerProperties.HTTP_METHOD_OVERRIDE))); }
@Override public void filter(final ContainerRequestContext requestContext) throws IOException { if (!denyAll) { if (rolesAllowed.length > 0 && !isAuthenticated(requestContext)) { throw new ForbiddenException(LocalizationMessages.USER_NOT_AUTHORIZED()); } for (final String role : rolesAllowed) { if (requestContext.getSecurityContext().isUserInRole(role)) { return; } } } throw new ForbiddenException(LocalizationMessages.USER_NOT_AUTHORIZED()); }
private static <T> void parseMappings(final String property, final String mappings, final Map<String, T> mappingsMap, final TypeParser<T> parser) { if (mappings == null) { return; } final String[] records = mappings.split(","); for (final String record : records) { final String[] mapping = record.split(":"); if (mapping.length != 2) { throw new IllegalArgumentException(LocalizationMessages.INVALID_MAPPING_FORMAT(property, mappings)); } final String trimmedSegment = mapping[0].trim(); final String trimmedValue = mapping[1].trim(); if (trimmedSegment.length() == 0) { throw new IllegalArgumentException(LocalizationMessages.INVALID_MAPPING_KEY_EMPTY(property, record)); } if (trimmedValue.length() == 0) { throw new IllegalArgumentException(LocalizationMessages.INVALID_MAPPING_VALUE_EMPTY(property, record)); } mappingsMap.put(trimmedSegment, parser.valueOf(trimmedValue)); } }
@Override public void filter(ContainerRequestContext rc) throws IOException { try { // Backward compatibility for Sidecars < 0.1.7 if (!rc.getHeaders().containsKey("X-Graylog-Collector-Version")) { super.filter(rc); } } catch (BadRequestException badRequestException) { throw new BadRequestException( "CSRF protection header is missing. Please add a \"" + HEADER_NAME + "\" header to your request.", badRequestException ); } } }
/** * Create a filter that reads the configuration (media type and language mappings) * from the provided {@link ResourceConfig} instance. * This constructor will be called by the Jersey runtime when the filter * class is returned from {@link javax.ws.rs.core.Application#getClasses()}. * The {@link ResourceConfig} instance will get auto-injected. * * @param rc ResourceConfig instance that holds the configuration for the filter. */ public UriConnegFilter(@Context final Configuration rc) { this(extractMediaTypeMappings(rc.getProperty(ServerProperties.MEDIA_TYPE_MAPPINGS)), extractLanguageMappings(rc.getProperty(ServerProperties.LANGUAGE_MAPPINGS))); }
private static Map<String, String> extractLanguageMappings(final Object mappings) { // parse and validate languageMappings set through LANGUAGE_MAPPINGS property return parseAndValidateMappings(ServerProperties.LANGUAGE_MAPPINGS, mappings, new TypeParser<String>() { public String valueOf(final String value) { return LanguageTag.valueOf(value).toString(); } }); }
/** * Initializes this filter setting the sources of information the filter should look for. * * @param sources Sources of method override information. If empty, both * {@link org.glassfish.jersey.server.filter.HttpMethodOverrideFilter.Source#HEADER} and * {@link org.glassfish.jersey.server.filter.HttpMethodOverrideFilter.Source#QUERY} will * be added to the config by default. */ public HttpMethodOverrideFilter(final Source... sources) { int c = 0; for (final Source cf : sources) { if (cf != null) { c |= cf.getFlag(); } } if (c == 0) { c = 3; } this.config = c; }
public static ContentEncoding fromString(String input) throws ParseException { HttpHeaderReader reader = HttpHeaderReader.newInstance(input); // Skip any white space reader.hasNext(); return new ContentEncoding(reader.nextToken().toString(), HttpHeaderReader.readQualityFactorParameter(reader)); }
/** * Create a filter that reads the configuration ({@link ServerProperties#HTTP_METHOD_OVERRIDE}) * from the provided {@link org.glassfish.jersey.server.ResourceConfig} instance. * <p/> * This constructor will be called by the Jersey runtime when the filter class is returned from * {@link javax.ws.rs.core.Application#getClasses()}. The {@link org.glassfish.jersey.server.ResourceConfig} * instance will get auto-injected. * * @param rc ResourceConfig instance that holds the configuration for the filter. */ public HttpMethodOverrideFilter(@Context final Configuration rc) { this(parseConfig(rc.getProperty(ServerProperties.HTTP_METHOD_OVERRIDE))); }
/** * Returns parameter value in a normalized form (uppercase, trimmed and {@code null} if empty string) * considering the config flags. * * @param source Config flag to look for (if set in the config, this method returns the param value, * if not set, this method returns {@code null}). * @param paramsMap Map to retrieve the parameter from. * @param paramName Name of the parameter to retrieve. * @return Normalized parameter value. Never returns an empty string - converts it to {@code null}. */ private String getParamValue(final Source source, final MultivaluedMap<String, String> paramsMap, final String paramName) { String value = source.isPresentIn(config) ? paramsMap.getFirst(paramName) : null; if (value == null) { return null; } value = value.trim(); return value.length() == 0 ? null : value.toUpperCase(); }
@Override public void filter(final ContainerRequestContext requestContext) throws IOException { if (!denyAll) { if (rolesAllowed.length > 0 && !isAuthenticated(requestContext)) { throw new ForbiddenException(LocalizationMessages.USER_NOT_AUTHORIZED()); } for (final String role : rolesAllowed) { if (requestContext.getSecurityContext().isUserInRole(role)) { return; } } } throw new ForbiddenException(LocalizationMessages.USER_NOT_AUTHORIZED()); }
private static <T> void parseMappings(final String property, final String mappings, final Map<String, T> mappingsMap, final TypeParser<T> parser) { if (mappings == null) { return; } final String[] records = mappings.split(","); for (final String record : records) { final String[] mapping = record.split(":"); if (mapping.length != 2) { throw new IllegalArgumentException(LocalizationMessages.INVALID_MAPPING_FORMAT(property, mappings)); } final String trimmedSegment = mapping[0].trim(); final String trimmedValue = mapping[1].trim(); if (trimmedSegment.length() == 0) { throw new IllegalArgumentException(LocalizationMessages.INVALID_MAPPING_KEY_EMPTY(property, record)); } if (trimmedValue.length() == 0) { throw new IllegalArgumentException(LocalizationMessages.INVALID_MAPPING_VALUE_EMPTY(property, record)); } mappingsMap.put(trimmedSegment, parser.valueOf(trimmedValue)); } }
private static Map<String, MediaType> extractMediaTypeMappings(final Object mappings) { // parse and validate mediaTypeMappings set through MEDIA_TYPE_MAPPINGS property return parseAndValidateMappings(ServerProperties.MEDIA_TYPE_MAPPINGS, mappings, new TypeParser<MediaType>() { public MediaType valueOf(final String value) { return MediaType.valueOf(value); } }); }
private static Map<String, String> extractLanguageMappings(final Object mappings) { // parse and validate languageMappings set through LANGUAGE_MAPPINGS property return parseAndValidateMappings(ServerProperties.LANGUAGE_MAPPINGS, mappings, new TypeParser<String>() { public String valueOf(final String value) { return LanguageTag.valueOf(value).toString(); } }); }