org.rapidoid.http.MediaType Java Examples

The following examples show how to use org.rapidoid.http.MediaType. 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: AbstractHttpHandler.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
protected String contentTypeInfo(String inside) {
	String type;

	if (contentType == MediaType.HTML_UTF_8) {
		type = options.mvc() ? "mvc" : "html";

	} else if (contentType == MediaType.JSON) {
		type = "json";

	} else if (contentType == MediaType.PLAIN_TEXT_UTF_8) {
		type = "plain";

	} else if (contentType == MediaType.BINARY) {
		type = "binary";

	} else {
		return inside;
	}

	return U.frmt("%s(%s)", type, inside);
}
 
Example #2
Source File: ResponseRenderer.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
public static byte[] renderMvc(ReqImpl req, Resp resp) {

		Object result = resp.result();
		String content = null;

		if (shouldRenderView(resp)) {
			boolean mandatory = (((RespImpl) resp).hasCustomView());

			ByteArrayOutputStream out = new ByteArrayOutputStream();
			boolean rendered = renderView(req, resp, result, mandatory, out);
			if (rendered) content = new String(out.toByteArray());
		}

		if (content == null) {
			Object respResult = U.or(result, "");
			content = new String(HttpUtils.responseToBytes(req, respResult, MediaType.HTML_UTF_8, null));
		}

		return renderPage(req, content);
	}
 
Example #3
Source File: PlaintextAndJsonServer.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected HttpStatus handle(Channel ctx, Buf buf, RapidoidHelper data) {

	if (data.isGet.value) {
		if (matches(buf, data.path, URI_PLAINTEXT)) {
			return ok(ctx, data.isKeepAlive.value, HELLO_WORLD, MediaType.TEXT_PLAIN);

		} else if (matches(buf, data.path, URI_JSON)) {
			return serializeToJson(HttpUtils.noReq(), ctx, data.isKeepAlive.value, new Message("Hello, World!"));
		}
	}

	return HttpStatus.NOT_FOUND;
}
 
Example #4
Source File: StaticResourcesHandler.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req) {

	if (!HttpUtils.isGetReq(req)) return HttpStatus.NOT_FOUND;

	try {
		String[] staticFilesLocations = customization.staticFilesPath();
		if (!U.isEmpty(staticFilesLocations)) {

			Res res = HttpUtils.staticResource(req, staticFilesLocations);
			if (res != null) {

				StaticFilesSecurity staticFilesSecurity = customization.staticFilesSecurity();

				if (staticFilesSecurity.canServe(req, res)) {
					byte[] bytes = res.getBytesOrNull();

					if (bytes != null) {
						MediaType contentType = U.or(MediaType.getByFileName(res.getName()), MediaType.BINARY);
						HttpIO.INSTANCE.write200(HttpUtils.maybe(req), ctx, isKeepAlive, contentType, bytes);
						return HttpStatus.DONE;
					}
				}
			}
		}

		return HttpStatus.NOT_FOUND;

	} catch (Exception e) {
		return HttpIO.INSTANCE.errorAndDone(req, e, LogLevel.ERROR);
	}
}
 
Example #5
Source File: Main.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

		/* Returning the request or response object means the response was constructed */

		On.get("/").html((Req req) -> {
			Resp resp = req.response();
			resp.contentType(MediaType.JSON);
			resp.result("hello");
			return resp;
		});
	}
 
Example #6
Source File: Main.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	/* The response type will be JSON, instead of HTML */

	On.get("/").html((Req req) -> {
		Resp resp = req.response();
		resp.contentType(MediaType.JSON);
		resp.result("abc");
		return resp;
	});
}
 
Example #7
Source File: OnRoute.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
private RouteOptions opts(MediaType contentType) {

		// don't change if already customized
		if (options.contentType() == null || !options.contentTypeCustomized()) {
			options.contentType(contentType);
		}

		return options;
	}
 
Example #8
Source File: CustomHttpServer.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpStatus handle(Channel ctx, Buf buf, RapidoidHelper req) {

	if (req.isGet.value) {
		if (matches(buf, req.path, URI_PLAINTEXT)) {
			return ok(ctx, req.isKeepAlive.value, HELLO_WORLD, MediaType.TEXT_PLAIN);

		} else if (matches(buf, req.path, URI_JSON)) {
			return serializeToJson(HttpUtils.noReq(), ctx, req.isKeepAlive.value, new Message("Hello, World!"));
		}
	}

	return HttpStatus.NOT_FOUND;
}
 
Example #9
Source File: MediaTypeTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomMediaType() {
	String[] attrss = {"abc=xy"};
	MediaType myType = MediaType.create("text/some", attrss, "some");

	eq(new String(myType.getBytes()), "text/some; abc=xy");

	eq(MediaType.getByFileName("abc.some"), myType);
	eq(MediaType.getByFileName(".some"), myType);
	eq(MediaType.getByFileName("some"), MediaType.DEFAULT);
	eq(MediaType.getByFileName("someX"), MediaType.DEFAULT);
}
 
Example #10
Source File: MediaTypeTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomMediaType2() {
	String[] attrss = {"a=1", "b=abc"};
	MediaType myType = MediaType.create("app/my", attrss, "my1", "my2");

	eq(new String(myType.getBytes()), "app/my; a=1; b=abc");

	eq(MediaType.getByFileExtension("my1"), myType);
	eq(MediaType.getByFileExtension("my2"), myType);
	eq(MediaType.getByFileExtension("myX"), null);
}
 
Example #11
Source File: OnRoute.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private RouteOptions plainOpts() {
	return opts(MediaType.PLAIN_TEXT_UTF_8);
}
 
Example #12
Source File: RouteImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isAPI() {
	if (options.mvc()) return false;

	return options.contentType() != MediaType.HTML_UTF_8;
}
 
Example #13
Source File: MediaTypeTest.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Test
public void testCommonMediaTypes() {
	eq(new String(MediaType.PLAIN_TEXT_UTF_8.getBytes()), "text/plain; charset=utf-8");
	eq(MediaType.getByFileExtension("txt"), MediaType.PLAIN_TEXT_UTF_8);

	eq(new String(MediaType.HTML_UTF_8.getBytes()), "text/html; charset=utf-8");
	eq(MediaType.getByFileExtension("html"), MediaType.HTML_UTF_8);

	eq(new String(MediaType.XHTML_XML_UTF8.getBytes()), "application/xhtml+xml; charset=utf-8");
	eq(MediaType.getByFileExtension("xhtml"), MediaType.XHTML_XML_UTF8);

	eq(new String(MediaType.CSS_UTF_8.getBytes()), "text/css; charset=utf-8");
	eq(MediaType.getByFileExtension("css"), MediaType.CSS_UTF_8);

	eq(new String(MediaType.JSON.getBytes()), "application/json");
	eq(MediaType.getByFileExtension("json"), MediaType.JSON);

	eq(new String(MediaType.JAVASCRIPT_UTF8.getBytes()), "application/javascript; charset=utf-8");
	eq(MediaType.getByFileExtension("js"), MediaType.JAVASCRIPT_UTF8);

	eq(new String(MediaType.PDF.getBytes()), "application/pdf");
	eq(MediaType.getByFileExtension("pdf"), MediaType.PDF);

	eq(new String(MediaType.ZIP.getBytes()), "application/zip");
	eq(MediaType.getByFileExtension("zip"), MediaType.ZIP);

	eq(new String(MediaType.SWF.getBytes()), "application/x-shockwave-flash");
	eq(MediaType.getByFileExtension("swf"), MediaType.SWF);

	eq(new String(MediaType.JPEG.getBytes()), "image/jpeg");
	eq(MediaType.getByFileExtension("jpg"), MediaType.JPEG);
	eq(MediaType.getByFileExtension("jpeg"), MediaType.JPEG);

	eq(new String(MediaType.PNG.getBytes()), "image/png");
	eq(MediaType.getByFileExtension("png"), MediaType.PNG);

	eq(new String(MediaType.GIF.getBytes()), "image/gif");
	eq(MediaType.getByFileExtension("gif"), MediaType.GIF);

	eq(new String(MediaType.BMP.getBytes()), "image/bmp");
	eq(MediaType.getByFileExtension("bmp"), MediaType.BMP);

	eq(new String(MediaType.SVG.getBytes()), "image/svg+xml; charset=utf-8");
	eq(MediaType.getByFileExtension("svg"), MediaType.SVG);
}
 
Example #14
Source File: OnRoute.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private RouteOptions htmlOpts() {
	return opts(MediaType.HTML_UTF_8);
}
 
Example #15
Source File: OnRoute.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private RouteOptions jsonOpts() {
	return opts(MediaType.JSON);
}
 
Example #16
Source File: OnRoute.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private RouteOptions xmlOpts() {
	return opts(MediaType.XML_UTF_8);
}
 
Example #17
Source File: RespImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Resp json(Object content) {
	return contentType(MediaType.JSON).result(content);
}
 
Example #18
Source File: OnRoute.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private RouteOptions mvcOpts() {
	return opts(MediaType.HTML_UTF_8).mvc(true);
}
 
Example #19
Source File: Main.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void setupSimpleHandlers() {
	On.get("/plaintext").managed(false).contentType(MediaType.TEXT_PLAIN).serve("Hello, world!");
	On.get("/json").managed(false).json(() -> new Message("Hello, world!"));
}
 
Example #20
Source File: RespImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Resp binary(Object content) {
	return contentType(MediaType.BINARY).result(content);
}
 
Example #21
Source File: ControllerCommon.java    From apollo with Apache License 2.0 4 votes vote down vote up
public static void assignJsonResponseToReq(Req req, int code, Object json) {
    req.response().code(code);
    req.response().contentType(MediaType.APPLICATION_JSON);
    req.response().json(json);
}
 
Example #22
Source File: RespImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Resp plain(Object content) {
	return contentType(MediaType.PLAIN_TEXT_UTF_8).result(content);
}
 
Example #23
Source File: RespImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Resp html(Object content) {
	return contentType(MediaType.HTML_UTF_8).result(content);
}
 
Example #24
Source File: RespImpl.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized MediaType contentType() {
	return this.contentType;
}
 
Example #25
Source File: RouteOptions.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public MediaType contentType() {
	return contentType;
}
 
Example #26
Source File: HttpIO.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public void respond(MaybeReq maybeReq, Channel channel, long connId, long handle,
                    int code, boolean isKeepAlive, MediaType contentType,
                    RespBody body, Map<String, String> headers, Map<String, String> cookies) {

	impl.respond(maybeReq, channel, connId, handle, code, isKeepAlive, contentType, body, headers, cookies);
}
 
Example #27
Source File: HttpIO.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public void writeHttpResp(MaybeReq req, Channel ctx, boolean isKeepAlive, int code, MediaType contentType, Object value) {
	impl.writeHttpResp(req, ctx, isKeepAlive, code, contentType, value);
}
 
Example #28
Source File: HttpIO.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public void write200(MaybeReq req, Channel ctx, boolean isKeepAlive, MediaType contentTypeHeader, byte[] content) {
	impl.write200(req, ctx, isKeepAlive, contentTypeHeader, content);
}
 
Example #29
Source File: HttpIO.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public void writeResponse(MaybeReq req, Channel ctx, boolean isKeepAlive, int code, MediaType contentTypeHeader, byte[] content) {
	impl.writeResponse(req, ctx, isKeepAlive, code, contentTypeHeader, content);
}
 
Example #30
Source File: CachedResp.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public CachedResp(int statusCode, MediaType contentType, Map<String, String> headers, ByteBuffer body) {
	this.statusCode = statusCode;
	this.contentType = contentType;
	this.headers = headers;
	this.body = body;
}