

package pl.edu.pwr.nlprestservice;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import pl.edu.pwr.nlptools.NLPTool;
import pl.edu.pwr.nlptools.NLPToolException;

/**
 * REST Web Service
 *
 * @author www_zsk
 */
@Path("syn")
public class Synchro {

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of Synchro
     */
    public Synchro() {
    }

    
    
    
    /**
     * Retrieves representation of an instance of pl.edu.pwr.nlprestservice.Synchro
     * @return an instance of java.lang.String
     */
    @GET
    @Path("/test")
    @Produces(MediaType.APPLICATION_XML)
    
    public String getXml() {
       StringBuilder sb=null;
          NLPTool nlpTool=new NLPTool("wcrft2");
          String id;
            try {
                id = nlpTool.saveText("ala ma kota");
                id=nlpTool.processFile(id);
                sb = nlpTool.readFile(id);
            } catch (NLPToolException ex) {
                throw new WebApplicationException(createResponse(ex, Response.Status.INTERNAL_SERVER_ERROR));
            }
          
       
          return new String(sb);
            
    }

    
    @GET
	@Path("{serviceName}/{data}")
        //@Produces(MediaType.TEXT_PLAIN)
       @Produces(MediaType.APPLICATION_XML)
	public String process(@PathParam("serviceName") String serviceName,@PathParam("data") String data)
        { StringBuilder sb=null;
          NLPTool nlpTool=new NLPTool(serviceName);
          String id;
            try {
                id = nlpTool.saveText(data);
                id=nlpTool.processText(id);
                sb = nlpTool.readFile(id);
            } catch (NLPToolException ex) {
                throw new WebApplicationException(createResponse(ex, Response.Status.INTERNAL_SERVER_ERROR));
            }
          
       
          return new String(sb);
            
        }
        
        @POST
	@Path("{serviceName}")
        //@Produces(MediaType.TEXT_PLAIN)
       @Produces(MediaType.APPLICATION_XML)
	public String processPost(@PathParam("serviceName") String serviceName, String data)
        { StringBuilder sb=null;
          NLPTool nlpTool=new NLPTool(serviceName);
          String id;
            try {
                id = nlpTool.saveText(data);
                id=nlpTool.processText(id);
                sb = nlpTool.readFile(id);
            } catch (NLPToolException ex) {
                throw new WebApplicationException(createResponse(ex, Response.Status.INTERNAL_SERVER_ERROR));
            }
          
       
          return new String(sb);
            
        }
        
        /* if exception message is provided, use it as it is;
     * if exception message is null, use fall back message 
     * (needs to be non-empty String in order to prevent 
     * HTTP container generated html message) */
    private Response createResponse(Exception ex, Response.Status status) {
        String message = ex.getMessage();
        if (message == null) {
            message = "Server internal error";
        }
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, message, ex);
        return Response.status(status).entity(message).type(MediaType.TEXT_PLAIN).build();
    }
}
