List of usage examples for io.vertx.core MultiMap set
@GenIgnore(GenIgnore.PERMITTED_TYPE) @Fluent MultiMap set(CharSequence name, Iterable<CharSequence> values);
From source file:com.hubrick.vertx.rest.converter.AbstractHttpMessageConverter.java
License:Apache License
@Override public void write(T object, MediaType contentType, HttpOutputMessage httpOutputMessage) throws HttpMessageConverterException { try {//from ww w . j a v a 2s . co m final MultiMap headers = httpOutputMessage.getHeaders(); if (headers.get(HttpHeaders.CONTENT_TYPE) == null) { MediaType contentTypeToUse = contentType; if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentTypeToUse = getDefaultContentType(object); } if (contentTypeToUse != null) { headers.set(HttpHeaders.CONTENT_TYPE, contentTypeToUse.toString()); } } writeInternal(object, httpOutputMessage); } catch (HttpMessageConverterException e) { throw e; } catch (Throwable t) { throw new HttpMessageConverterException("Error writing object", t); } }
From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java
License:Open Source License
/** * Create all required header so content can be cache by Caching servers or Browsers * * @param request base HttpServerRequest * @param props file properties//from ww w . j a v a2 s.c o m */ private void writeCacheHeaders(HttpServerRequest request, FileProps props) { MultiMap headers = request.response().headers(); if (cachingEnabled) { // We use cache-control and last-modified // We *do not use* etags and expires (since they do the same thing - redundant) headers.set("cache-control", "public, max-age=" + maxAgeSeconds); headers.set("last-modified", dateTimeFormatter.format(props.lastModifiedTime())); } // date header is mandatory headers.set("date", dateTimeFormatter.format(new Date())); }
From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java
License:Open Source License
private void sendFile(RoutingContext context, String file, FileProps fileProps) { HttpServerRequest request = context.request(); Long offset = null;/*from ww w.j a va 2s.co m*/ Long end = null; MultiMap headers = null; if (rangeSupport) { // check if the client is making a range request String range = request.getHeader("Range"); // end byte is length - 1 end = fileProps.size() - 1; if (range != null) { Matcher m = RANGE.matcher(range); if (m.matches()) { try { String part = m.group(1); // offset cannot be empty offset = Long.parseLong(part); // offset must fall inside the limits of the file if (offset < 0 || offset >= fileProps.size()) { throw new IndexOutOfBoundsException(); } // length can be empty part = m.group(2); if (part != null && part.length() > 0) { // ranges are inclusive end = Long.parseLong(part); // offset must fall inside the limits of the file if (end < offset || end >= fileProps.size()) { throw new IndexOutOfBoundsException(); } } } catch (NumberFormatException | IndexOutOfBoundsException e) { context.fail(REQUESTED_RANGE_NOT_SATISFIABLE.code()); return; } } } // notify client we support range requests headers = request.response().headers(); headers.set("Accept-Ranges", "bytes"); // send the content length even for HEAD requests headers.set("Content-Length", Long.toString(end + 1 - (offset == null ? 0 : offset))); } writeCacheHeaders(request, fileProps); if (request.method() == HttpMethod.HEAD) { request.response().end(); } else { if (rangeSupport && offset != null) { // must return content range headers.set("Content-Range", "bytes " + offset + "-" + end + "/" + fileProps.size()); // return a partial response request.response().setStatusCode(PARTIAL_CONTENT.code()); // Wrap the sendFile operation into a TCCL switch, so the file resolver would find the file from the set // classloader (if any). final Long finalOffset = offset; final Long finalEnd = end; wrapInTCCLSwitch(() -> request.response().sendFile(file, finalOffset, finalEnd + 1, res2 -> { if (res2.failed()) { context.fail(res2.cause()); } }), null); } else { // Wrap the sendFile operation into a TCCL switch, so the file resolver would find the file from the set // classloader (if any). wrapInTCCLSwitch(() -> request.response().sendFile(file, res2 -> { if (res2.failed()) { context.fail(res2.cause()); } }), null); } } }
From source file:examples.HTTPExamples.java
License:Open Source License
public void example21(HttpServerRequest request) { HttpServerResponse response = request.response(); MultiMap headers = response.headers(); headers.set("content-type", "text/html"); headers.set("other-header", "wibble"); }
From source file:examples.HTTPExamples.java
License:Open Source License
public void example24(HttpServerRequest request) { HttpServerResponse response = request.response(); response.setChunked(true);/*from www . j a v a2s . c om*/ MultiMap trailers = response.trailers(); trailers.set("X-wibble", "woobble").set("X-quux", "flooble"); }
From source file:examples.HTTPExamples.java
License:Open Source License
public void example37(HttpClientRequest request) { // Write some headers using the headers() multimap MultiMap headers = request.headers(); headers.set("content-type", "application/json").set("other-header", "foo"); }
From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.BasicAuth.java
License:Apache License
@Override public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap, Handler<AsyncResult<Void>> resultHandler) { headerMap.set("Authorization", Basic.encode(config.get("username"), config.get("password"))); resultHandler.handle(Future.succeededFuture()); return this; }
From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.KeycloakOAuth2.java
License:Apache License
@Override public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap, Handler<AsyncResult<Void>> resultHandler) { OAuth2FlowType flowType = getFlowType(config.get("flowType")); JsonObject params = new JsonObject(); if (config.get("username") != null) { params.put("username", config.get("username")); }// www .j a v a 2s .c o m if (config.get("password") != null) { params.put("password", config.get("password")); } OAuth2Auth oauth2 = KeycloakAuth.create(vertx, flowType, mapToJson(config)); oauth2.getToken(params, tokenResult -> { if (tokenResult.succeeded()) { log.debug("OAuth2 Keycloak exchange succeeded."); AccessToken token = tokenResult.result(); headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token")); resultHandler.handle(Future.succeededFuture()); } else { log.error("Access Token Error: {0}.", tokenResult.cause().getMessage()); resultHandler.handle(Future.failedFuture(tokenResult.cause())); } }); return this; }
From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.KeycloakOAuth2Client.java
License:Apache License
@Override public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap, AsyncResultHandler<Void> resultHandler) { OAuth2FlowType flowType = getFlowType(config.get("flowType")); JsonObject params = new JsonObject(); if (config.get("username") != null) { params.put("username", config.get("username")); }//from w ww .ja v a2s .c om if (config.get("password") != null) { params.put("password", config.get("password")); } OAuth2Auth oauth2 = OAuth2Auth.createKeycloak(vertx, flowType, mapToJson(config)); oauth2.getToken(params, tokenResult -> { if (tokenResult.succeeded()) { log.debug("OAuth2 Keycloak exchange succeeded."); AccessToken token = tokenResult.result(); headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token")); resultHandler.handle(Future.succeededFuture()); } else { log.error("Access Token Error: {0}.", tokenResult.cause().getMessage()); resultHandler.handle(Future.failedFuture(tokenResult.cause())); } }); return this; }
From source file:io.apiman.gateway.engine.vertx.polling.fetchers.auth.OAuth2.java
License:Apache License
@Override public Authenticator authenticate(Vertx vertx, Map<String, String> config, MultiMap headerMap, Handler<AsyncResult<Void>> resultHandler) { OAuth2ClientOptions credentials = new OAuth2ClientOptions(mapToJson(config)); if (config.get("oauthUri") != null) { credentials.setSite(config.get("oauthUri")); }//from ww w. j a v a 2 s .co m if (config.get("clientId") != null) { credentials.setClientID(config.get("clientId")); } OAuth2FlowType flowType = getFlowType(config.get("flowType")); JsonObject params = new JsonObject(); if (config.get("username") != null) { params.put("username", config.get("username")); } if (config.get("password") != null) { params.put("password", config.get("password")); } OAuth2Auth oauth2 = OAuth2Auth.create(vertx, flowType, credentials); oauth2.getToken(params, tokenResult -> { if (tokenResult.succeeded()) { log.debug("OAuth2 exchange succeeded."); AccessToken token = tokenResult.result(); headerMap.set("Authorization", "Bearer " + token.principal().getString("access_token")); resultHandler.handle(Future.succeededFuture()); } else { log.error("Access Token Error: {0}.", tokenResult.cause().getMessage()); resultHandler.handle(Future.failedFuture(tokenResult.cause())); } }); return this; }