/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */
package com.wegas.core.rest;

import com.wegas.core.Helper;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Set;
import javax.ejb.Stateless;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.*;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * This servlet allows to retrieve several resources in a single request. Used
 * to combine .js and .css files.
 *
 * @todo Resulting files should be cached. For example check
 * https://github.com/smaring/javascript-combo-service/blob/master/src/main/java/org/maring/util/js/JavascriptComboService.java
 *
 * @author Francois-Xavier Aeberhard <fx@red-agent.com>
 */
@Stateless
@Path("combo")
public class ComboController {

    final static private String MediaTypeCss = "text/css; charset=ISO-8859-1";
    final static private String MediaTypeJs = "text/javascript; charset=ISO-8859-1";
    private static final Logger logger = LoggerFactory.getLogger(ComboController.class);
    /**
     *
     */
    @Context
    protected UriInfo uriInfo;
    /**
     *
     */
    @Context
    private ServletContext servletContext;

    /**
     * Retrieve
     *
     * @param req
     * @return
     * @throws IOException
     */
    @GET
    @Produces({MediaTypeJs, MediaTypeCss})
    public Response index(@Context Request req) throws IOException {
        final Set<String> files = this.uriInfo.getQueryParameters().keySet();
        files.remove("v");
        files.remove("version");
        final String mediaType = (files.iterator().next().endsWith("css"))
                ? MediaTypeCss : MediaTypeJs;                            // Select the content-type based on the first file extension



        // MediaType types[] = {"application/json", "application/xml"};
        // List<Variant> vars = Variant.mediaTypes(types).add().build();
        // Variant var = req.selectVariant(vars);

        final CacheControl cc = new CacheControl();
        //cc.setMaxAge(60000);
        //cc.setPrivate(true);
        //cc.setNoTransform(true);
        //cc.setMustRevalidate(false);
        //cc.setNoCache(false);

        //EntityTag etag = new EntityTag();
        //Response.ResponseBuilder responseBuilder = request.evaluatePreconditions(updateTimestamp, etag);

        return Response.ok(this.getCombinedFile(files, mediaType)).
                type(mediaType).
                cacheControl(cc).
                expires(new Date(System.currentTimeMillis() + (1000 * 60 * 60 * 24 * 3))).
                build();
    }

    private String getCombinedFile(Set<String> fileList, String mediaType) throws IOException {
        StringBuilder acc = new StringBuilder();
        for (String fileName : fileList) {
            try {
                InputStream fis = (InputStream) servletContext.getResourceAsStream(fileName);
                String content = IOUtils.toString(fis, Helper.getWegasProperty("encoding"));
                //String content = new Scanner(fis, Helper.getWegasProperty("encoding"))
                //.useDelimiter("\\A").next();                              // Use a fake delimiter to read all lines at once

                if (mediaType.equals(MediaTypeCss)) {                       // @hack for css files, we correct the path
                    String dir = fileName.substring(0, fileName.lastIndexOf('/') + 1);
                    content = content.replaceAll("url\\(\"?\'?([^:\\)\"]+)\"?\'?\\)",
                            "url(" + servletContext.getContextPath()
                            + dir + "$1)");              //Regexp to avoid rewriting protocol guess they contain ':' (http: data:)
                }
                acc.append(content);
            } catch (NullPointerException e) {
                logger.error("Resource not found : {}", fileName);
            }
        }
        return acc.toString();
    }
}

@@ -258,16 +258,44 @@
 Stream;%0A
+import java.util.ArrayList;%0A
 import j
@@ -326,18 +326,19 @@
 va.util.
-Se
+Lis
 t;%0Aimpor
@@ -1651,24 +1651,25 @@
 ption %7B%0A
+%0A
         
 final Se
@@ -1656,24 +1656,26 @@
  %7B%0A%0A        
+//
 final Set%3CSt
@@ -1731,16 +1731,450 @@
 eySet();
+ // Old version, removed cause query parameters where in the wrong order%0A%0A        ArrayList%3CString%3E files = new ArrayList%3C%3E();                            // New version, with parameters in the right order%0A        for (String parameter : this.uriInfo.getRequestUri().getQuery().split(%22&%22)) %7B%0A            String split = parameter.split(%22=%22)%5B0%5D;%0A            if (split != null) %7B%0A                files.add(split);%0A            %7D%0A        %7D%0A
 %0A       
@@ -2222,24 +2222,25 @@
 %22version%22);%0A
+%0A
         fina
@@ -2437,18 +2437,16 @@
 ension%0A%0A
-%0A%0A
         
@@ -3299,18 +3299,19 @@
 nedFile(
-Se
+Lis
 t%3CString

Fix ComboController return order