package org.pdfserv;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.springframework.stereotype.Component;

@Component
@Path("/document")
@Produces(MediaType.APPLICATION_JSON)
public class DocumentResource {
	
	private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

	@GET
	public List<String> documentList() {
    	return Arrays.asList(
    			Application.DOCUMENT_FOLDER.list( 
    					(File dir, String name) -> { return name.endsWith("pdf") || name.endsWith("PDF"); }
    			)
    		);
    }

	private File openFile(String documentName) {
		return new File(Application.DOCUMENT_FOLDER, documentName);
	}
	
	private int readPageCount(File documentFile) throws IOException {
		try (PDDocument pdfDocument = PDDocument.load(documentFile)) {
			return pdfDocument.getNumberOfPages();
		}
	}
	
	private Date getLastModified(File documentFile) {
		return new Date(documentFile.lastModified());
	}
	
	private EntityTag createETag(File documentFile) {
		return new EntityTag(documentFile.lastModified() + "");
	}
	
	@GET
	@Path("/{documentName}")
	public Response getDocument(@PathParam("documentName") String documentName) throws IOException {
		File documentFile = openFile(documentName);
		if (!documentFile.exists()) {
			throw new NotFoundException();
		}
		Document document = new Document();
		document.setName(documentName);
		document.setLastModified(DATE_FORMAT.format(getLastModified(documentFile)));
		document.setPageCount(readPageCount(documentFile));
		return Response.ok(document).tag(createETag(documentFile)).build();
	}
}

@@ -317,16 +317,49 @@
 oduces;%0A
+import javax.ws.rs.core.Context;%0A
 import j
@@ -420,16 +420,49 @@
 iaType;%0A
+import javax.ws.rs.core.Request;%0A
 import j
@@ -1534,168 +1534,170 @@
 %0A%09%0A%09
-@GET%0A%09@Path(%22/%7BdocumentName%7D%22)%0A%09public Response getDocument(@PathParam(%22documentName%22) String documentName) throws IOException %7B%0A%09%09File d
+private boolean isUnchanged(Request request, EntityTag eTag) %7B%0A%09%09return request.evaluatePreconditions(eTag) != null;%0A%09%7D%0A%09%0A%09private D
 ocument
-File = openFile(
+ createDocument(String 
 docu
@@ -1708,18 +1708,15 @@
 Name
-);%0A%09%09if (!
+, File 
 docu
@@ -1727,42 +1727,19 @@
 File
-.exists()) %7B%0A%09%09%09throw new NotFound
+) throws IO
 Exce
@@ -1743,23 +1743,18 @@
 xception
-();%0A%09%09%7D
+ %7B
 %0A%09%09Docum
@@ -1964,68 +1964,521 @@
 urn 
-Response.ok(document).tag(createETag(documentFile)).build();
+document;%0A%09%7D%0A%09%0A%09@GET%0A%09@Path(%22/%7BdocumentName%7D%22)%0A%09public Response getDocument(@PathParam(%22documentName%22) String documentName, @Context Request request) throws IOException %7B%0A%09%09File documentFile = openFile(documentName);%0A%09%09if (!documentFile.exists()) %7B%0A%09%09%09throw new NotFoundException();%0A%09%09%7D%0A%09%09EntityTag eTag = createETag(documentFile);%0A%09%09if (isUnchanged(request, eTag)) %7B%0A%09%09%09return Response.notModified(eTag).build();%0A%09%09%7D else %7B%0A%09%09%09return Response.ok(createDocument(documentName, documentFile)).tag(eTag).build();%0A%09%09%7D
 %0A%09%7D%0A

Send not modifed header to client, if requested document has not been changed.