class PigResource {

	@POST
	@Path("/script/{scriptName}")
	@Consumes ( {MediaType.APPLICATION_OCTET_STREAM} )
	@Produces ({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
	public Response registerScript(@PathParam("scriptName") String scriptName, InputStream uploadedScript) throws IOException {
		logger.info("Request for registering script " + scriptName);
		if (uploadedScript == null || uploadedScript.available() == 0){
			logger.error("Empty input stream passed");
			return Response
			    .status(HttpURLConnection.HTTP_BAD_REQUEST)
			    .entity("Bad request. No script uploaded!")
			    .build();
		}
			   
		StringWriter writer = new StringWriter();
		IOUtils.copy(uploadedScript, writer, "UTF-8");
				
		String script= writer.toString();
		if (PigScriptValidator.validatePigScript(script)) {
			InputStream stream= new ByteArrayInputStream(script.getBytes());
			String pathName= PropertyLoader.getInstance().getProperty(Constants.SCRIPTS_PATH) + scriptName;
			PigJobServerImpl.getPigJobServer().registerFile(pathName, stream);
			return Response.ok().entity(pathName).build();
		}
		logger.info("Script " + scriptName + " is not valid");
		return Response
		    .status(HttpURLConnection.HTTP_BAD_REQUEST)
		    .entity("Bad Request. Either DUMP command is used or STORE is used without '$output'")
		    .build();
	}

}