view src/main/java/de/mpiwg/gazetteer/rest/GetFileText.java @ 8:10b6e9d96d59

Changing the URL of the extraction interface.
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Tue, 17 Mar 2015 10:51:19 +0100
parents 7682c04c63a8
children
line wrap: on
line source

package de.mpiwg.gazetteer.rest;

import java.io.PrintWriter;

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

import org.json.JSONObject;

import de.mpiwg.gazetteer.bo.LGFile;
import de.mpiwg.gazetteer.utils.DataProvider;
import de.mpiwg.gazetteer.utils.FileManager;
import de.mpiwg.gazetteer.utils.exceptions.GazetteerException;

public class GetFileText extends AbstractServletMethod {

	public static String name = "getFileText";
	
	public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Long fileId = getQueryLongParam(request, "fileId");
		
		if(fileId != null){
			LGFile file = DataProvider.getInstance().getFile(fileId);
			if(file != null){
				
				String text = FileManager.getFile(file);
				PrintWriter out = response.getWriter();
				out.print(text);
				out.flush();
				response.setContentType("text/plain; charset=UTF-8");
				
			}else{
				response.setContentType("application/json");
				JSONObject json = new JSONObject();
				json.put("status", "error");
				json.put("message", "File no found (" + fileId + ")");
				json.put("code", GazetteerException.CODE);
				PrintWriter out = response.getWriter();
				out.print(json.toString());
				out.flush();
			}
		}else{
			response.setContentType("application/json");
			JSONObject json = new JSONObject();
			json.put("status", "error");
			json.put("message", "Following parameters are mandatory: fileId.");
			PrintWriter out = response.getWriter();
			out.print(json.toString());
			out.flush();
		}
	}
	
}