package io.mandrel.data.export;

import io.mandrel.common.data.Spider;
import io.mandrel.data.content.WebPageExtractor;
import io.mandrel.data.spider.SpiderService;

import java.util.Optional;

import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ExporterService {

	private final SpiderService spiderService;

	@Autowired
	public ExporterService(SpiderService spiderService) {
		super();
		this.spiderService = spiderService;
	}

	public void export(Long id, String extractorName, DocumentExporter exporter, HttpServletResponse response) {
		Optional<Spider> optional = spiderService.get(id);

		if (optional.isPresent()) {
			Spider spider = optional.get();
			spiderService.injectAndInit(spider);
			Optional<WebPageExtractor> extractor = spider.getExtractors().getPages().stream().filter(ext -> ext.getName().equals(extractorName)).findFirst();
			if (extractor.isPresent()) {
				response.setContentType(exporter.contentType());
				try {
					exporter.init(response.getWriter());
					extractor.get().getDataStore().byPages(id, 1000, data -> {
						try {
							exporter.export(data, extractor.get().getFields());
						} catch (Exception e) {
							log.debug("Uhhh...", e);
							return false;
						}
						return CollectionUtils.isNotEmpty(data);
					});
				} catch (Exception e) {
					log.debug("Uhhh...", e);
				} finally {
					try {
						exporter.close();
					} catch (Exception e1) {
						log.debug("Uhhh...", e1);
					}
				}
			} else {
				notFound(response);
				log.debug("Extract {} not found for spider {}", extractorName, id);
			}
		} else {
			notFound(response);
			log.debug("Spider {} not found", id);
		}
	}

	public void export(Long id, RawExporter exporter, HttpServletResponse response) {
		Optional<Spider> optional = spiderService.get(id);

		if (optional.isPresent()) {
			Spider spider = optional.get();
			spiderService.injectAndInit(spider);

			response.setContentType(exporter.contentType());
			try {
				exporter.init(response.getWriter());
				spider.getStores().getPageStore().byPages(id, 1000, data -> {
					try {
						exporter.export(data);
					} catch (Exception e) {
						log.debug("Uhhh...", e);
						return false;
					}
					return CollectionUtils.isNotEmpty(data);
				});
			} catch (Exception e) {
				log.debug("Uhhh...", e);
			} finally {
				try {
					exporter.close();
				} catch (Exception e1) {
					log.debug("Uhhh...", e1);
				}
			}
		} else {
			notFound(response);
			log.debug("Spider {} not found", id);
		}

	}

	private void notFound(HttpServletResponse response) {
		response.setStatus(HttpStatus.NOT_FOUND.value());
	}
}

@@ -26,16 +26,60 @@
 xport;%0A%0A
+import io.mandrel.common.NotFoundException;%0A
 import i
@@ -199,24 +199,47 @@
 erService;%0A%0A
+import java.io.Writer;%0A
 import java.
@@ -258,56 +258,8 @@
 l;%0A%0A
-import javax.servlet.http.HttpServletResponse;%0A%0A
 impo
@@ -411,52 +411,8 @@
 ed;%0A
-import org.springframework.http.HttpStatus;%0A
 impo
@@ -754,36 +754,21 @@
 er, 
-HttpServletResponse response
+Writer writer
 ) %7B%0A
@@ -1111,61 +1111,8 @@
 ) %7B%0A
-%09%09%09%09response.setContentType(exporter.contentType());%0A
 %09%09%09%09
@@ -1136,36 +1136,22 @@
 er.init(
-response.getW
+w
 riter
-()
 );%0A%09%09%09%09%09
@@ -1632,32 +1632,45 @@
 %09%09%09notFound(
-response
+%22Extractor not found%22
 );%0A%09%09%09%09log.d
@@ -1756,32 +1756,42 @@
 %09%09%09notFound(
-response
+%22Spider not found%22
 );%0A%09%09%09log.de
@@ -1885,36 +1885,21 @@
 er, 
-HttpServletResponse response
+Writer writer
 ) %7B%0A
@@ -2062,60 +2062,8 @@
 );%0A%0A
-%09%09%09response.setContentType(exporter.contentType());%0A
 %09%09%09t
@@ -2089,28 +2089,14 @@
 nit(
-response.getW
+w
 riter
-()
 );%0A%09
@@ -2539,24 +2539,34 @@
 otFound(
-response
+%22Spider not found%22
 );%0A%09%09%09lo
@@ -2637,89 +2637,63 @@
 und(
-HttpServletResponse response) %7B%0A%09%09response.setStatus(HttpStatus.NOT_FOUND.value()
+String message) %7B%0A%09%09throw new NotFoundException(message
 );%0A%09

Update extraction