package com.github.dandelion.extras.spring3.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.dandelion.core.asset.web.HtmlUtil;
import com.github.dandelion.core.asset.web.data.AssetContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.dandelion.core.asset.AssetType;
import com.github.dandelion.core.asset.cache.AssetsCacheSystem;
import com.github.dandelion.core.asset.web.AssetServlet;

/**
 * <p>
 * Controller used to render cached web resources instead of a concrete
 * implementation of {@link AssetServlet}.
 * <p>
 * Note that client applications must import the corresponding Spring config
 * file in order for this controller to be scanned. For example using:
 * 
 * <pre>
 * <code>&lt;import resource="classpath:dandelion.xml" /&gt;</code>
 * </pre>
 * 
 * @since 0.3.0
 */
@Controller
@RequestMapping(value = AssetServlet.DANDELION_ASSETS_URL, method = RequestMethod.GET)
public class AssetController {
	@RequestMapping(value = "{assetKey:.+}")
	public @ResponseBody
    String renderAsset(@PathVariable String assetKey, HttpServletResponse response) throws IOException {
        AssetContent assetContent = HtmlUtil.getAssetContent(assetKey);
        response.setHeader("Cache-Control", HtmlUtil.getCacheControl());
        response.setContentType(assetContent.getContentType());
		return assetContent.getContent();
	}
}

@@ -366,16 +366,196 @@
 actory;%0A
+import org.springframework.http.HttpHeaders;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.http.ResponseEntity;%0A
 import o
@@ -1596,17 +1596,16 @@
 lic 
-@
 Response
 Body
@@ -1604,23 +1604,22 @@
 onse
-Body%0A    
+Entity%3C
 String
+%3E
  ren
@@ -1793,44 +1793,170 @@
     
-response.setHeader(%22
+HttpHeaders headers = new HttpHeaders();%0A        headers.setContentType(MediaType.parseMediaType(assetContent.getContentType()));%0A        headers.set
 Cache
--
 Control
-%22, 
+(
 Html
@@ -1988,24 +1988,23 @@
         
-response
+headers
 .setCont
@@ -2006,20 +2006,22 @@
 tContent
-Type
+Length
 (assetCo
@@ -2040,50 +2040,116 @@
 tent
-Type());%0A%09%09return assetContent.getContent(
+().getBytes().length);%0A%09%09return new ResponseEntity%3CString%3E(assetContent.getContent(), headers, HttpStatus.OK
 );%0A%09

set properly the response headers in spring controller