/*
 * 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.server.http.jetty.jersey.server.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Resource {

    private static final Logger log = LoggerFactory.getLogger(Resource.class);

    private final File rootDir;

    private List<String> welcomeFiles = new ArrayList<>(Arrays.asList("index.html"));
    private boolean allowDirectoryListing = true;
    private String context = "/static";
    private List<String> resourcePaths = new ArrayList<>();

    public Resource(File rootDir) {
        this.rootDir = rootDir;
    }

    public Resource addResourcePath(String path) {
        resourcePaths.add(path);
        return this;
    }

    public Resource setWelcomeFiles(List<String> welcomeFiles) {
        this.welcomeFiles = welcomeFiles;
        return this;
    }

    public Resource setDirectoryListingAllowed(boolean allowDirectoryListing) {
        this.allowDirectoryListing = allowDirectoryListing;
        return this;
    }

    public Resource setContext(String context) {
        this.context = context;
        return this;
    }

    public String getContext() {
        return context;
    }

    public ResourceHandler getResourceHandler() {
        ResourceHandler handler = new ResourceHandler();

        String resourceBase = null;

        for (String path : resourcePaths) {
            File dir = new File(rootDir, path);
            if (dir.isDirectory() && dir.list().length > 0) {
                resourceBase = dir.getAbsolutePath();
                break;
            }
        }

        if (resourceBase == null) {
            throw new IllegalStateException("Could not find resourceBase for context " + this.context);
        }

        log.info("Assigning " + resourceBase + " to context " + this.context);
        handler.setResourceBase(resourceBase);
        handler.setWelcomeFiles(welcomeFiles.toArray(new String[welcomeFiles.size()]));
        handler.setDirectoriesListed(allowDirectoryListing);
        handler.setCacheControl("max-age=21600"); //6hrs
        handler.setEtags(true);

        return handler;
    }
}

@@ -658,16 +658,56 @@
 .util;%0A%0A
+import com.google.common.collect.Lists;%0A
 import j
@@ -751,33 +751,8 @@
 st;%0A
-import java.util.Arrays;%0A
 impo
@@ -1067,33 +1067,22 @@
 s = 
-new ArrayList%3C%3E(
+Lists.new
 Array
-s.as
 List
@@ -1095,17 +1095,16 @@
 x.html%22)
-)
 ;%0A    pr
@@ -1246,16 +1246,81 @@
 ist%3C%3E();
+%0A    private List%3CString%3E classpathResources = new ArrayList%3C%3E();
 %0A%0A    pu
@@ -1435,24 +1435,39 @@
 ing path) %7B%0A
+        // wtf%0A
         reso
@@ -1507,32 +1507,204 @@
 rn this;%0A    %7D%0A%0A
+    public Resource addClasspathResource(String classpathResource) %7B%0A        // this is dumb%0A        classpathResources.add(classpathResource);%0A        return this;%0A    %7D%0A%0A
     public Resou
@@ -2306,16 +2306,86 @@
  = null;
+%0A        org.eclipse.jetty.util.resource.Resource baseResource = null;
 %0A%0A      
@@ -2466,24 +2466,50 @@
 Dir, path);%0A
+            // seriously?%0A
             
@@ -2692,24 +2692,432 @@
  == null) %7B%0A
+            for (String classpathResource : classpathResources) %7B%0A                baseResource = org.eclipse.jetty.util.resource.Resource.newClassPathResource(classpathResource);%0A                // this sucks%0A                if (baseResource != null && baseResource.exists()) %7B%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (resourceBase == null && baseResource == null) %7B%0A
             
@@ -3177,16 +3177,32 @@
 rceBase 
+or baseResource 
 for cont
@@ -3227,32 +3227,127 @@
 xt);%0A        %7D%0A%0A
+        // the things I do for backwards compatibility%0A        if (resourceBase != null) %7B%0A    
         log.info
@@ -3409,32 +3409,36 @@
 ntext);%0A        
+    
 handler.setResou
@@ -3456,24 +3456,219 @@
 ourceBase);%0A
+        %7D else %7B // baseResource != null%0A            log.info(%22Assigning %22 + baseResource.getName() + %22 to context %22 + this.context);%0A            handler.setBaseResource(baseResource);%0A        %7D%0A
         hand

allow classpath resources