package eu.cloudopting.web.rest;

import java.io.IOException;
import java.io.InputStream;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.commons.JcrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import eu.cloudopting.store.StoreService;

@RestController
@RequestMapping("/api")
public class JcrImageResource {

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

	@Inject
	private StoreService storeService;
	
	@Inject
	private Session session;
    
    /**
     * JackRabbit Repository URL
     */
    @Value("${spring.jcr.repo_http}")
	private String jrHttp;
    
    private String getRelativePathForNode(String jcrPath){
    	if (log!=null) log.debug("JcrImageResource.getRelativePathForNode starting with jcrPath: " + jcrPath);
    	String result = null;
    	String jrRepositoryBase = "http://lab1.cloudopting.org:8083/repository/default/";
		StoreService ss = this.getStoreService();
		if (ss!=null){
			jrRepositoryBase = ss.getJrHttp();
		}else{
			String s = "JcrImageResource.getRelativePathForNode. Store Service NOT injected!";
			if (log!=null){
				log.error(s);
			}
		}
		result = StringUtils.removeStart(jcrPath, jrRepositoryBase); 
		if (!result.startsWith("/")){
			result = "/"+result;
		}
		if (log!=null) log.debug("JcrImageResource.getRelativePathForNode ended with result: " + result);
		return result;
    }
	
	@RequestMapping(value = "/jr/img", method = RequestMethod.GET)
	public final ResponseEntity<InputStreamResource> getJcrImage(@RequestParam("jcrPath") String jcrPath) {
		if (log!=null) log.debug("JcrImageResource.getJcrImage starting with jcrPath: " + jcrPath);
		ResponseEntity<InputStreamResource> result = null;
		String relativePath = this.getRelativePathForNode(jcrPath);
		InputStream is = null;
		try {
			Session s = this.getSession();
			Node n = JcrUtils.getNodeIfExists(relativePath, s);
			is = JcrUtils.readFile(n);
			Node jcrContent = n.getNode("jcr:content");
			long size = jcrContent.getProperty("jcr:data").getBinary().getSize();
			HttpHeaders respHeaders = new HttpHeaders();
			String mType = jcrContent.getProperty("jcr:mimeType").getString();
			respHeaders.setContentType(MediaType.parseMediaType(mType));
			respHeaders.setContentLength(size);
			InputStreamResource isr = new InputStreamResource(is);
			result = new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK);
		} catch (RepositoryException e) {
			e.printStackTrace();
			log.error("JcrImageResource.getJcrImage. Repository Exception", e);
			result = new ResponseEntity<InputStreamResource>(new InputStreamResource(is), new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
		} finally {
			try {
				if (is!=null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
				log.warn("JcrImageResource.getJcrImage. Repository Exception", e);
			}
		}
		if (log!=null) log.debug("JcrImageResource.getJcrImage ended with result: " + result);
	    return result;
	}
	
	@RequestMapping(value = "/jr/img", method = RequestMethod.DELETE)
	public final Boolean deleteJcrImage(@RequestParam("jcrPath") String jcrPath) {
		if (log!=null) log.debug("JcrImageResource.deleteJcrImage starting with jcrPath: " + jcrPath);
		Boolean result = false;
		String relativePath = this.getRelativePathForNode(jcrPath);
		try {
			Session s = this.getSession();
			s.removeItem(relativePath);
			s.save();
			result = true;
		} catch (RepositoryException e) {
			e.printStackTrace();
			log.error("JcrImageResource.deleteJcrImage. Repository Exception", e);
			result = false;
		}
		if (log!=null) log.debug("JcrImageResource.deleteJcrImage ended with result: " + result);
	    return result;
	}
	
	public Logger getLog() {
		return log;
	}

	public StoreService getStoreService() {
		return storeService;
	}
	
	public Session getSession() {
		return session;
	}
}
@@ -3346,16 +3346,21 @@
 R);%0A%09%09%7D 
+%0A//%09%09
 finally 
@@ -3361,16 +3361,18 @@
 nally %7B%0A
+//
 %09%09%09try %7B
@@ -3372,16 +3372,18 @@
 %09%09try %7B%0A
+//
 %09%09%09%09if (
@@ -3392,16 +3392,18 @@
 !=null)%0A
+//
 %09%09%09%09%09is.
@@ -3411,16 +3411,18 @@
 lose();%0A
+//
 %09%09%09%7D cat
@@ -3442,16 +3442,18 @@
 on e) %7B%0A
+//
 %09%09%09%09e.pr
@@ -3469,16 +3469,18 @@
 race();%0A
+//
 %09%09%09%09log.
@@ -3542,21 +3542,25 @@
 n%22, e);%0A
+//
 %09%09%09%7D%0A
+//
 %09%09%7D%0A%09%09if

Fix for image retrieval from JR