public static void proxyResponseHeaders(Map<String, String> respHeaders, SimpleHttpResp resp) { for (Map.Entry<String, String> hdr : respHeaders.entrySet()) { String name = hdr.getKey(); String value = hdr.getValue(); if (name.equalsIgnoreCase("Content-type")) { resp.contentType = MediaType.of(value); } else if (name.equalsIgnoreCase("Set-Cookie")) { String[] parts = value.split("=", 2); U.must(parts.length == 2, "Invalid value of the Set-Cookie header!"); if (resp.cookies == null) { resp.cookies = U.map(); } resp.cookies.put(parts[0], parts[1]); } else if (!ignoreResponseHeaderInProxy(name)) { if (resp.headers == null) { resp.headers = U.map(); } resp.headers.put(name, value); } } }
public static void setContentTypeForFile(Resp resp, File file) { U.must(file.exists()); setContentType(resp, MediaType.getByFileName(file.getAbsolutePath())); }
public String info() { if (this == HTML_UTF_8) { return "html"; } if (this == JSON) { return "json"; } if (this == PLAIN_TEXT_UTF_8) { return "plain"; } if (this == BINARY) { return "binary"; } return toString(); }
private void writeContentTypeHeader(Channel ctx, MediaType contentType) { ctx.write(CONTENT_TYPE_TXT); ctx.write(contentType.getBytes()); ctx.write(CR_LF); }
private static HttpResponseRenderer mediaResponseRenderer(Resp resp) { Customization customization = Customization.of(resp.request()); if (resp.contentType().equals(MediaType.JSON)) { return customization.jsonResponseRenderer(); } else if (resp.contentType().equals(MediaType.XML_UTF_8)) { return customization.xmlResponseRenderer(); } else { // defaults to json return customization.jsonResponseRenderer(); } }
private void addDefaultHeaders(Channel ctx, boolean isKeepAlive, MediaType contentType) { if (!isKeepAlive || MANDATORY_HEADER_CONNECTION) { ctx.write(isKeepAlive ? CONN_KEEP_ALIVE : CONN_CLOSE); } if (MANDATORY_HEADER_SERVER) { ctx.write(SERVER_HEADER); } if (MANDATORY_HEADER_DATE) { ctx.write(DATE_IS); if (!GlobalCfg.uniformOutput()) { ctx.write(Dates.getDateTimeBytes()); } else { ctx.write(UNIFORM_DATE); } ctx.write(CR_LF); } if (MANDATORY_HEADER_CONTENT_TYPE) { ctx.write(contentType.asHttpHeader()); } }
public static MediaType of(String contentType) { return new MediaType(contentType); }
/*******************************************************/ public static synchronized MediaType create(String type, String... fileExtensisons) { return create(type, NO_ATTR, fileExtensisons); }
public static synchronized MediaType getByFileName(String filename) { int dotPos = filename.lastIndexOf('.'); if (dotPos >= 0) { String ext = filename.substring(dotPos + 1); return getByFileExtension(ext); } else { return MediaType.DEFAULT; } }
private Map<String, Object> defaultResponses(Route route) { RouteMeta meta = route.config().meta(); Map<String, Object> responses = U.map(); Map<String, Object> schema = U.notEmpty(meta.outputSchema()) ? meta.outputSchema().toOpenAPISchema() : U.map(); MediaType mediaType = U.or(route.config().contentType(), MediaType.JSON); String contentType = new String(mediaType.getBytes()); Map<String, Object> ok = U.map( "description", "success", "content", U.map( contentType, schema ) ); responses.put("200", ok); Map<String, Object> error = U.map( "description", "unexpected error", "content", U.map( contentType, OpenAPIModel.schemaRef("Error") ) ); responses.put("default", error); return responses; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RouteOptions that = (RouteOptions) o; if (mvc != that.mvc) return false; if (managed != that.managed) return false; if (cacheTTL != that.cacheTTL) return false; if (cacheCapacity != that.cacheCapacity) return false; if (contentType != null ? !contentType.equals(that.contentType) : that.contentType != null) return false; if (view != null ? !view.equals(that.view) : that.view != null) return false; if (zone != null ? !zone.equals(that.zone) : that.zone != null) return false; if (transaction != that.transaction) return false; if (roles != null ? !roles.equals(that.roles) : that.roles != null) return false; return Arrays.equals(wrappers, that.wrappers); }
private void addDefaultHeaders(Channel ctx, boolean isKeepAlive, MediaType contentType) { if (!isKeepAlive || MANDATORY_HEADER_CONNECTION) { ctx.write(isKeepAlive ? CONN_KEEP_ALIVE : CONN_CLOSE); } if (MANDATORY_HEADER_SERVER) { ctx.write(SERVER_HEADER); } if (MANDATORY_HEADER_DATE) { ctx.write(DATE_IS); if (!GlobalCfg.uniformOutput()) { ctx.write(Dates.getDateTimeBytes()); } else { ctx.write(UNIFORM_DATE); } ctx.write(CR_LF); } if (MANDATORY_HEADER_CONTENT_TYPE) { ctx.write(contentType.asHttpHeader()); } }
public static synchronized MediaType create(String type, String[] attributes, String... fileExtensisons) { MediaType mt = new MediaType(type, attributes); for (String ext : fileExtensisons) { FILE_EXTENSISONS.put(ext, mt); } return mt; }
public static synchronized MediaType createUTF8(String type, String... fileExtensisons) { return create(type, UTF8_ATTR, fileExtensisons); }
private void writeContentTypeHeader(Channel ctx, MediaType contentType) { ctx.write(CONTENT_TYPE_TXT); ctx.write(contentType.getBytes()); ctx.write(CR_LF); }
public static void proxyResponseHeaders(Map<String, String> respHeaders, SimpleHttpResp resp) { for (Map.Entry<String, String> hdr : respHeaders.entrySet()) { String name = hdr.getKey(); String value = hdr.getValue(); if (name.equalsIgnoreCase("Content-type")) { resp.contentType = MediaType.of(value); } else if (name.equalsIgnoreCase("Set-Cookie")) { String[] parts = value.split("=", 2); U.must(parts.length == 2, "Invalid value of the Set-Cookie header!"); if (resp.cookies == null) { resp.cookies = U.map(); } resp.cookies.put(parts[0], parts[1]); } else if (!ignoreResponseHeaderInProxy(name)) { if (resp.headers == null) { resp.headers = U.map(); } resp.headers.put(name, value); } } }
public static void setContentTypeForResource(Resp resp, Res resource) { U.must(resource.exists()); setContentType(resp, MediaType.getByFileName(resource.getName())); }
private static HttpResponseRenderer mediaResponseRenderer(Resp resp) { Customization customization = Customization.of(resp.request()); if (resp.contentType().equals(MediaType.JSON)) { return customization.jsonResponseRenderer(); } else if (resp.contentType().equals(MediaType.XML_UTF_8)) { return customization.xmlResponseRenderer(); } else { // defaults to json return customization.jsonResponseRenderer(); } }
break; case "POST": request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse(req.contentType().toString()), req.body())).build(); break; case "PUT": request = new Request.Builder().url(url).put(RequestBody.create(MediaType.parse(req.contentType().toString()), req.body())).build(); break; case "DELETE": request = new Request.Builder().url(url).delete(RequestBody.create(MediaType.parse(req.contentType().toString()), req.body())).build(); break; case "HEAD":
private void addRoute(T config, HttpVerb verb, String uri) { OnRoute route = addRoute(verb, uri); if (config.contentType != null) route.contentType(MediaType.of(config.contentType)); if (config.managed != null) route.managed(config.managed); if (config.transaction != null) route.transaction(config.transaction); if (config.cacheTTL != null) route.cacheTTL(config.cacheTTL); if (config.cacheCapacity != null) route.cacheCapacity(config.cacheCapacity); if (config.roles != null) { route.roles(config.roles); App.boot().auth(); } addHandler(config, uri, route); }