<commit_before>/*
 * Copyright (c) 2014-2015 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.werval.filters;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import io.werval.api.context.Context;
import io.werval.api.filters.FilterChain;
import io.werval.api.filters.FilterWith;
import io.werval.api.http.Headers;
import io.werval.api.outcomes.Outcome;
import java.util.Arrays;

/**
 * Accept Content Types Annotation.
 * <p>
 * A request with a content-type not listed will be rejected with a {@literal 400 Bad Request}.
 *
 * @navassoc 1 apply 1 Filter
 */
@FilterWith( AcceptContentTypes.Filter.class )
@Target( { ElementType.METHOD, ElementType.TYPE } )
@Retention( RetentionPolicy.RUNTIME )
@Inherited
@Documented
public @interface AcceptContentTypes
{
    /**
     * @return Accepted content-types, all others will lead to a 400 Bad Request
     */
    String[] value();

    /**
     * Accept Content Types Filter.
     */
    public static class Filter
        implements io.werval.api.filters.Filter<AcceptContentTypes>
    {
        @Override
        public CompletableFuture<Outcome> filter(
            FilterChain chain,
            Context context,
            Optional<AcceptContentTypes> annotation
        )
        {
            String[] acceptedContentTypes = annotation.map( annot -> annot.value() ).orElse( new String[ 0 ] );
            Optional<String> contentType = context.request().contentType().map( Headers::extractContentMimeType ).get();
            if( contentType.isPresent() )
            {
                if( Arrays.stream( acceptedContentTypes ).anyMatch( ct -> ct.equals( contentType.get() ) ) )
                {
                    return chain.next( context );<commit_msg>@AcceptedContentType bug #242 came back a bit differently, fix this<commit_after>/*
 * Copyright (c) 2014-2015 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.werval.filters;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import io.werval.api.context.Context;
import io.werval.api.filters.FilterChain;
import io.werval.api.filters.FilterWith;
import io.werval.api.http.Headers;
import io.werval.api.outcomes.Outcome;
import java.util.Arrays;

/**
 * Accept Content Types Annotation.
 * <p>
 * A request with a content-type not listed will be rejected with a {@literal 400 Bad Request}.
 *
 * @navassoc 1 apply 1 Filter
 */
@FilterWith( AcceptContentTypes.Filter.class )
@Target( { ElementType.METHOD, ElementType.TYPE } )
@Retention( RetentionPolicy.RUNTIME )
@Inherited
@Documented
public @interface AcceptContentTypes
{
    /**
     * @return Accepted content-types, all others will lead to a 400 Bad Request
     */
    String[] value();

    /**
     * Accept Content Types Filter.
     */
    public static class Filter
        implements io.werval.api.filters.Filter<AcceptContentTypes>
    {
        @Override
        public CompletableFuture<Outcome> filter(
            FilterChain chain,
            Context context,
            Optional<AcceptContentTypes> annotation
        )
        {
            String[] acceptedContentTypes = annotation.map( annot -> annot.value() ).orElse( new String[ 0 ] );
            Optional<String> contentType = context.request().contentType()
                .map( Headers::extractContentMimeType )
                .orElse( Optional.empty() );
            if( contentType.isPresent() )
            {
                if( Arrays.stream( acceptedContentTypes ).anyMatch( ct -> ct.equals( contentType.get() ) ) )
                {
                    return chain.next( context );
{'fraction_non_alphanumeric': 0.0973536797372996, 'fraction_numerical': 0.008692292833687464, 'mean_word_length': 3.132482043096568, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4047885', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1387, 'n_words': 441}