Java Code Examples for org.glassfish.grizzly.http.server.Response#setContentType()

The following examples show how to use org.glassfish.grizzly.http.server.Response#setContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GatfConfigToolUtil.java    From gatf with Apache License 2.0 5 votes vote down vote up
protected static void handleErrorJson(Throwable e, Response response, HttpStatus status) throws IOException
{
	String configJson = e.getMessage()==null?ExceptionUtils.getStackTrace(e):e.getMessage();
	response.setContentType(MediaType.APPLICATION_XML);
	response.setContentLength(configJson.length());
       response.getWriter().write(configJson);
	response.setStatus(status==null?HttpStatus.INTERNAL_SERVER_ERROR_500:status);
	if(status==null)e.printStackTrace();
}
 
Example 2
Source File: TintHandler.java    From tint with GNU General Public License v3.0 4 votes vote down vote up
public void writeOutput(Response response, String contentType, String output) throws IOException {
    response.setContentType(contentType);
    response.setCharacterEncoding("UTF-8");
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.getWriter().write(output);
}
 
Example 3
Source File: VanillaExtract.java    From osm-lib with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void service(Request request, Response response) throws Exception {

    response.setContentType("application/osm");
    String uri = request.getDecodedRequestURI();
    LOG.info("VEX request: {}", uri);
    int suffixIndex = uri.lastIndexOf('.');
    String fileType = uri.substring(suffixIndex);
    OutputStream outStream = response.getOutputStream();
    try {
        String[] coords = uri.substring(1, suffixIndex).split("[,;]");
        double minLat = Double.parseDouble(coords[0]);
        double minLon = Double.parseDouble(coords[1]);
        double maxLat = Double.parseDouble(coords[2]);
        double maxLon = Double.parseDouble(coords[3]);
        if (minLat >= maxLat || minLon >= maxLon || minLat < -90 || maxLat > 90 || minLon < -180 || maxLon > 180) {
            throw new IllegalArgumentException();
        }
        /* Respond to head requests to let the client know the server is alive and the request is valid. */
        if (request.getMethod() == Method.HEAD) {
            response.setStatus(HttpStatus.OK_200);
            return;
        }
        /* TODO filter out buildings on the server side. */
        boolean buildings = coords.length > 4 && "buildings".equalsIgnoreCase(coords[4]);

        OSMEntitySink sink = OSMEntitySink.forStream(uri, outStream);
        TileOSMSource tileSource = new TileOSMSource(osm);
        tileSource.setBoundingBox(minLat, minLon, maxLat, maxLon);
        tileSource.copyTo(sink);
        response.setStatus(HttpStatus.OK_200);
    } catch (Exception ex) {
        LOG.error("Could not process request with bad URI format {}.", uri);
        response.setContentType("text/plain");
        response.setStatus(HttpStatus.BAD_REQUEST_400);
        outStream.write("URI format: /min_lat,min_lon,max_lat,max_lon[.pbf|.vex] (all coords in decimal degrees)\n".getBytes());
        ex.printStackTrace();
    } finally {
        outStream.close();
    }
}
 
Example 4
Source File: GatfReportsHandler.java    From gatf with Apache License 2.0 4 votes vote down vote up
@Override
public void service(Request request, Response response) throws Exception {
    response.setHeader("Cache-Control", "no-cache, no-store");
   	try {
   		final GatfExecutorConfig gatfConfig = GatfConfigToolUtil.getGatfExecutorConfig(mojo, null);
		String basepath = gatfConfig.getOutFilesBasePath()==null?mojo.getRootDir():gatfConfig.getOutFilesBasePath();
		String dirPath = basepath + SystemUtils.FILE_SEPARATOR + gatfConfig.getOutFilesDir();
		if(!new File(dirPath).exists()) {
			new File(dirPath).mkdir();
		}
		if(request.getMethod().equals(Method.GET) ) {
		    new CacheLessStaticHttpHandler(dirPath).service(request, response);
		} else if(request.getMethod().equals(Method.PUT) ) {
		    String action = request.getParameter("action");
		    String testcaseFileName = request.getParameter("testcaseFileName");
		    String testCaseName = request.getParameter("testCaseName");
		    boolean isServerLogsApi = request.getParameter("isServerLogsApi")!=null;
		    boolean isExternalLogsApi = request.getParameter("isExternalLogsApi")!=null;
		    TestCaseReport tcReport = null;
		    if(action.equals("replayTest"))
		    { 
		        tcReport = new org.codehaus.jackson.map.ObjectMapper().readValue(request.getInputStream(), 
		                TestCaseReport.class);
		        if(tcReport == null) {
		            throw new RuntimeException("Invalid testcase report details provided");
		        }
		    } 
		    else if(isExternalLogsApi && !action.equals("playTest"))
		    {
		        tcReport = new org.codehaus.jackson.map.ObjectMapper().readValue(request.getInputStream(), 
		                TestCaseReport.class);
		    }
		    Object[] out = executeTest(gatfConfig, tcReport, action, testcaseFileName, testCaseName, isServerLogsApi, isExternalLogsApi, 0, false);
		    if(out[1]!=null) {
		        response.setContentType(out[2].toString());
		        response.setContentLength(((String)out[1]).length());
		        response.getWriter().write((String)out[1]);
		    }
		    response.setStatus((HttpStatus)out[0]);
		}
   	} catch (Exception e) {
   		GatfConfigToolUtil.handleError(e, response, null);
	} finally {
	    if(lock.isLocked()) {
	        lock.unlock();
	    }
	}
   }
 
Example 5
Source File: PlainText2HttpHandler.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void service(final Request request, final Response response) throws Exception {
	response.setContentType(CONTENT_TYPE);
	response.getOutputStream().write(HELLO_WORLD_BYTES);
}
 
Example 6
Source File: JsonHttpHandler.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void service(final Request request, final Response response) throws Exception {
	response.setContentType(CONTENT_TYPE);
	// Write JSON encoded message to the response.
	MAPPER.writeValue(response.getOutputStream(), new HelloMessage());
}
 
Example 7
Source File: PlainTextHttpHandler.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void service(final Request request, final Response response) throws Exception {
	response.setContentType(CONTENT_TYPE);
	response.getWriter().write("Hello, World!");
}