/*
 * Copyright 2013 Jive Software, Inc
 *
 * 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 com.jivesoftware.os.routing.bird.server.filter;

import com.jivesoftware.os.mlogger.core.MetricLogger;
import com.jivesoftware.os.mlogger.core.MetricLoggerFactory;
import com.jivesoftware.os.routing.bird.oauth.AuthValidator;
import com.jivesoftware.os.routing.bird.oauth.AuthValidatorHelper;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.oauth1.signature.OAuth1Request;
import org.glassfish.jersey.oauth1.signature.OAuth1Signature;
import org.glassfish.jersey.server.oauth1.internal.OAuthServerRequest;

public class OAuthRequestFilter implements ContainerRequestFilter {

    private static final MetricLogger LOG = MetricLoggerFactory.getLogger();

    private static final Response UNAUTHORIZED = Response.status(Status.UNAUTHORIZED).entity("OAuth validation failed").build();

    private final List<Pattern> patterns;
    private final AuthValidator<OAuth1Signature, OAuth1Request> authValidator;
    private final OAuth1Signature verifier;

    public OAuthRequestFilter(List<Pattern> patterns, AuthValidator<OAuth1Signature, OAuth1Request> authValidator, OAuth1Signature verifier) {
        this.patterns = patterns;
        this.authValidator = authValidator;
        this.verifier = verifier;
    }

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        try {
            if (authValidator != null && verifier != null) {
                boolean accepted = false;
                if (patterns == null) {
                    accepted = true;
                } else {
                    for (Pattern pattern : patterns) {
                        if (pattern.matcher(requestContext.getUriInfo().getPath()).matches()) {
                            accepted = true;
                            break;
                        }
                    }
                }
                if (accepted) {
                    OAuthServerRequest serverRequest = new OAuthServerRequest(requestContext);
                    if (AuthValidatorHelper.isValid(authValidator, verifier, serverRequest, null, UNAUTHORIZED) == UNAUTHORIZED) {
                        requestContext.abortWith(UNAUTHORIZED);
                    }
                }
            }
        } catch (Exception e) {
            LOG.error("Failed to check authentication", e);
            throw e;
        }
    }
}

@@ -648,16 +648,56 @@
 ilter;%0A%0A
+import com.google.common.collect.Lists;%0A
 import c
@@ -1671,50 +1671,8 @@
 );%0A%0A
-    private final List%3CPattern%3E patterns;%0A
     
@@ -1790,22 +1790,64 @@
 rifier;%0A
-%0A
     p
+rivate final List%3CPattern%3E patterns;%0A%0A    p
 ublic OA
@@ -1867,32 +1867,8 @@
 ter(
-List%3CPattern%3E patterns, 
 Auth
@@ -1952,45 +1952,28 @@
 fier
-) %7B%0A        this.patterns = patterns;
+, String... paths) %7B
 %0A   
@@ -2043,24 +2043,190 @@
 = verifier;%0A
+        this.patterns = Lists.newArrayListWithCapacity(paths.length);%0A%0A        for (String path : paths) %7B%0A            patterns.add(Pattern.compile(path));%0A        %7D%0A
     %7D%0A%0A    @
@@ -2397,156 +2397,8 @@
 ) %7B%0A
-                boolean accepted = false;%0A                if (patterns == null) %7B%0A                    accepted = true;%0A                %7D else %7B%0A    
     
@@ -2440,28 +2440,24 @@
 patterns) %7B%0A
-    
             
@@ -2544,182 +2544,8 @@
     
-                        accepted = true;%0A                            break;%0A                        %7D%0A                    %7D%0A                %7D%0A                if (accepted) %7B%0A
     
@@ -2635,16 +2635,20 @@
 ntext);%0A
+    
         
@@ -2794,16 +2794,20 @@
         
+    
 requestC
@@ -2838,16 +2838,73 @@
 RIZED);%0A
+                        %7D%0A                        break;%0A
         

simplify request filter construction