List of usage examples for io.vertx.core MultiMap get
@Nullable String get(String name);
From source file:org.sfs.nodes.data.CanReadVolume.java
License:Apache License
@Override public void handle(SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsDataNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(new ValidateParamExists(VOLUME)).flatMap(httpServerRequest1 -> { MultiMap params = httpServerRequest1.params(); String volumeId = params.get(VOLUME); VolumeManager volumeManager = vertxContext.verticle().nodes().volumeManager(); LocalNode localNode = new LocalNode(vertxContext, volumeManager); return localNode.canReadVolume(volumeId); }).single().subscribe(new Terminus<Boolean>(httpServerRequest) { @Override/*from w w w . j a va2s . co m*/ public void onNext(Boolean canContinue) { if (TRUE.equals(canContinue)) { httpServerRequest.response().setStatusCode(HTTP_OK); } else { httpServerRequest.response().setStatusCode(HTTP_NOT_ACCEPTABLE); } } }); }
From source file:org.sfs.nodes.data.CanWriteVolume.java
License:Apache License
@Override public void handle(SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsDataNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(new ValidateParamExists(VOLUME)).flatMap(httpServerRequest1 -> { MultiMap params = httpServerRequest1.params(); String volumeId = params.get(VOLUME); VolumeManager volumeManager = vertxContext.verticle().nodes().volumeManager(); LocalNode localNode = new LocalNode(vertxContext, volumeManager); return localNode.canWriteVolume(volumeId); }).single().subscribe(new Terminus<Boolean>(httpServerRequest) { @Override//from ww w.j a va 2 s . c o m public void onNext(Boolean canContinue) { if (TRUE.equals(canContinue)) { httpServerRequest.response().setStatusCode(HTTP_OK); } else { httpServerRequest.response().setStatusCode(HTTP_NOT_ACCEPTABLE); } } }); }
From source file:org.sfs.nodes.data.ChecksumBlob.java
License:Apache License
@Override public void handle(final SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsDataNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(new ValidateParamExists(VOLUME)).map(new ValidateParamExists(POSITION)) .map(new ValidateParamBetweenLong(POSITION, 0, MAX_VALUE)) .map(new ValidateParamBetweenLong(LENGTH, 0, MAX_VALUE)) .map(new ValidateParamBetweenLong(OFFSET, 0, MAX_VALUE)).map(new ValidateParamComputedDigest()) .flatMap(httpServerRequest1 -> { MultiMap params = httpServerRequest1.params(); final Iterable<MessageDigestFactory> iterable = from(params.names()) .transform(new Function<String, Optional<MessageDigestFactory>>() { @Override public Optional<MessageDigestFactory> apply(String param) { Matcher matcher = COMPUTED_DIGEST.matcher(param); if (matcher.matches()) { String digestName = matcher.group(1); return fromValueIfExists(digestName); }//from www .j av a 2 s . co m return absent(); } }).filter(Optional::isPresent).transform(input -> input.get()); MultiMap queryParams = httpServerRequest1.params(); String volumeId = queryParams.get(VOLUME); long position = parseLong(queryParams.get(POSITION)); Optional<Long> oOffset; if (queryParams.contains(OFFSET)) { oOffset = of(parseLong(queryParams.get(OFFSET))); } else { oOffset = absent(); } Optional<Long> oLength; if (queryParams.contains(LENGTH)) { oLength = of(parseLong(queryParams.get(LENGTH))); } else { oLength = absent(); } // let the client know we're alive by sending pings on the response stream httpServerRequest1.startProxyKeepAlive(); LocalNode localNode = new LocalNode(vertxContext, vertxContext.verticle().nodes().volumeManager()); return localNode .checksum(volumeId, position, oOffset, oLength, toArray(iterable, MessageDigestFactory.class)) .map(digestBlobOptional -> new Holder2<>(httpServerRequest1, digestBlobOptional)); }).flatMap(holder -> httpServerRequest.stopKeepAlive().map(aVoid -> holder)) .onErrorResumeNext(throwable -> httpServerRequest.stopKeepAlive() .flatMap(aVoid -> Observable.<Holder2<SfsRequest, Optional<DigestBlob>>>error(throwable))) .single().onErrorResumeNext(new HandleServerToBusy<>()) .subscribe(new Terminus<Holder2<SfsRequest, Optional<DigestBlob>>>(httpServerRequest) { @Override public void onNext(Holder2<SfsRequest, Optional<DigestBlob>> holder) { Optional<DigestBlob> oJsonDigestBlob = holder.value1(); JsonObject jsonResponse = new JsonObject(); if (oJsonDigestBlob.isPresent()) { jsonResponse.put("code", HTTP_OK).put("blob", oJsonDigestBlob.get().toJsonObject()); } else { jsonResponse.put("code", HTTP_NOT_FOUND); } HttpServerResponse httpResponse = holder.value0().response(); httpResponse.write(jsonResponse.encode(), UTF_8.toString()).write(DELIMITER_BUFFER); } }); }
From source file:org.sfs.nodes.data.DeleteBlob.java
License:Apache License
@Override public void handle(final SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsDataNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(httpServerRequestServiceContextHolder2 -> httpServerRequest) .map(new ValidateParamExists(VOLUME)).map(new ValidateParamExists(POSITION)) .map(new ValidateParamBetweenLong(POSITION, 0, MAX_VALUE)).flatMap(httpServerRequest1 -> { MultiMap headers = httpServerRequest1.params(); String volumeId = headers.get(VOLUME); long position = parseLong(headers.get(POSITION)); final Volume volume = vertxContext.verticle().nodes().volumeManager().get(volumeId).get(); return volume.delete(httpServerRequest1.vertxContext().vertx(), position); }).map(headerBlobOptional -> new Holder2<>(httpServerRequest, headerBlobOptional)) .map(new WriteHeaderBlobAsHttpResponseHeaders<>()).single() .onErrorResumeNext(new HandleServerToBusy<>()) .subscribe(new Terminus<Holder2<SfsRequest, Optional<HeaderBlob>>>(httpServerRequest) { @Override/*w w w . j av a 2s . c o m*/ public void onNext(Holder2<SfsRequest, Optional<HeaderBlob>> holder) { Optional<HeaderBlob> oBlob = holder.value1(); if (oBlob.isPresent()) { holder.value0().response().setStatusCode(HTTP_NO_CONTENT); } else { holder.value0().response().setStatusCode(HTTP_NOT_MODIFIED); } } }); }
From source file:org.sfs.nodes.data.GetBlob.java
License:Apache License
@Override public void handle(final SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsDataNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(new ValidateParamExists(VOLUME)).map(new ValidateParamExists(POSITION)) .map(new ValidateParamBetweenLong(POSITION, 0, MAX_VALUE)) .map(new ValidateParamBetweenLong(LENGTH, 0, MAX_VALUE)) .map(new ValidateParamBetweenLong(OFFSET, 0, MAX_VALUE)).flatMap(httpServerRequest1 -> { httpServerRequest1.response().setChunked(true); MultiMap queryParams = httpServerRequest1.params(); String volumeId = queryParams.get(VOLUME); long position = parseLong(queryParams.get(POSITION)); Optional<Long> oOffset; if (queryParams.contains(OFFSET)) { oOffset = of(parseLong(queryParams.get(OFFSET))); } else { oOffset = absent();/*from w ww . j av a 2 s . c o m*/ } Optional<Long> oLength; if (queryParams.contains(LENGTH)) { oLength = of(parseLong(queryParams.get(LENGTH))); } else { oLength = absent(); } final Volume volume = vertxContext.verticle().nodes().volumeManager().get(volumeId).get(); return volume .getDataStream(httpServerRequest1.vertxContext().vertx(), position, oOffset, oLength) .map(readStreamBlob -> new Holder2<>(httpServerRequest1, readStreamBlob)); }).map(new WriteHeaderBlobAsHttpResponseHeaders<>()) .map(new WriteReadStreamBlobAsHttpResponseHeaders<>()).flatMap(input -> { Optional<ReadStreamBlob> oReadStreamBlob = input.value1(); if (oReadStreamBlob.isPresent()) { ReadStreamBlob readStreamBlob = oReadStreamBlob.get(); HttpServerResponse httpServerResponse = input.value0().response(); httpServerResponse.setStatusCode(HTTP_OK); NoEndEndableWriteStream endableWriteStream = new NoEndEndableWriteStream( new HttpServerResponseEndableWriteStream(httpServerResponse)); return readStreamBlob.produce(endableWriteStream); } else { input.value0().response().setStatusCode(HTTP_NOT_FOUND); } return aVoid(); }).single().onErrorResumeNext(new HandleServerToBusy<>()) .subscribe(new Terminus<Void>(httpServerRequest) { @Override public void onNext(Void aVoid) { // do nothing here since we had to write the status before the stream was written } }); }
From source file:org.sfs.nodes.data.PutBlob.java
License:Apache License
@Override public void handle(final SfsRequest httpServerRequest) { httpServerRequest.pause();/* www .j av a2s .c o m*/ VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsDataNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(new ValidateParamExists(VOLUME)).map(new ValidateHeaderExists(CONTENT_LENGTH)) .map(new ValidateHeaderBetweenLong(CONTENT_LENGTH, 0, MAX_VALUE)) .map(new ValidateParamComputedDigest()).map(new ToVoid<>()).map(aVoid -> httpServerRequest) .flatMap(httpServerRequest1 -> { MultiMap headers = httpServerRequest1.headers(); MultiMap params = httpServerRequest1.params(); String volumeId = params.get(VOLUME); final Iterable<MessageDigestFactory> iterable = from(params.names()) .transform(new Function<String, Optional<MessageDigestFactory>>() { @Override public Optional<MessageDigestFactory> apply(String param) { Matcher matcher = COMPUTED_DIGEST.matcher(param); if (matcher.find()) { String digestName = matcher.group(1); return fromValueIfExists(digestName); } return absent(); } }).filter(Optional::isPresent).transform(input -> input.get()); VolumeManager volumeManager = vertxContext.verticle().nodes().volumeManager(); if (!volumeManager.isOpen()) { throw new HttpStatusCodeException("VolumeManager not open", HTTP_UNAVAILABLE); } Volume volume = volumeManager.get(volumeId).orNull(); if (volume == null) { throw new HttpStatusCodeException(String.format("Volume %s not found", volumeId), HTTP_UNAVAILABLE); } if (!Volume.Status.STARTED.equals(volume.status())) { throw new HttpStatusCodeException(String.format("Volume %s not started", volumeId), HTTP_UNAVAILABLE); } long length = parseLong(headers.get(CONTENT_LENGTH)); httpServerRequest1.startProxyKeepAlive(); return volume.putDataStream(httpServerRequest1.vertxContext().vertx(), length) .flatMap(writeStreamBlob -> { DigestReadStream digestReadStream = new DigestReadStream(httpServerRequest1, toArray(iterable, MessageDigestFactory.class)); CountingReadStream countingReadStream = new CountingReadStream(digestReadStream); return writeStreamBlob.consume(countingReadStream).map(aVoid1 -> { DigestBlob digestBlob = new DigestBlob(writeStreamBlob.getVolume(), writeStreamBlob.getPosition(), countingReadStream.count()); for (Holder2<MessageDigestFactory, byte[]> digest : digestReadStream .digests()) { digestBlob.withDigest(digest.value0(), digest.value1()); } return new Holder2<>(httpServerRequest1, of(digestBlob)); }); }); }).single().onErrorResumeNext(new HandleServerToBusy<>()) .subscribe(new Terminus<Holder2<SfsRequest, Optional<DigestBlob>>>(httpServerRequest) { @Override public void onNext(Holder2<SfsRequest, Optional<DigestBlob>> holder) { Optional<DigestBlob> oJsonDigestBlob = holder.value1(); JsonObject jsonResponse = new JsonObject(); if (oJsonDigestBlob.isPresent()) { jsonResponse.put("code", HTTP_OK).put("blob", oJsonDigestBlob.get().toJsonObject()); } else { jsonResponse.put("code", HTTP_INTERNAL_ERROR); } HttpServerResponse httpResponse = holder.value0().response(); httpResponse.write(jsonResponse.encode(), UTF_8.toString()).write(DELIMITER_BUFFER); } }); }
From source file:org.sfs.nodes.master.MasterNodeExecuteJob.java
License:Apache License
@Override public void handle(SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); Defer.aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsMasterNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(new ValidateHeaderExists(Jobs.Parameters.JOB_ID)) .map(new ValidateHeaderBetweenLong(Jobs.Parameters.TIMEOUT, 100, Long.MAX_VALUE)) .map(new ToVoid<>()).flatMap(aVoid -> { MultiMap headers = httpServerRequest.headers(); String jobId = headers.get(JOB_ID); long timeout = headers.contains(Jobs.Parameters.TIMEOUT) ? Long.parseLong(headers.get(Jobs.Parameters.TIMEOUT)) : -1;/* w ww . ja va2s .c om*/ httpServerRequest.startProxyKeepAlive(); Jobs jobs = vertxContext.verticle().jobs(); return Defer.aVoid().doOnNext( aVoid1 -> jobs.execute(vertxContext, jobId, headers).subscribe(new NullSubscriber<>())) .flatMap(aVoid1 -> { if (timeout >= 0) { return jobs.waitStopped(vertxContext, jobId, timeout, TimeUnit.MILLISECONDS); } else { return Defer.aVoid(); } }); }).single().subscribe(new Terminus<Void>(httpServerRequest) { @Override public void onNext(Void aVoid) { JsonObject responseJson = new JsonObject().put("code", HTTP_OK).put("message", "Success"); httpServerRequest.response().write(responseJson.encode(), StandardCharsets.UTF_8.toString()) .write(DELIMITER_BUFFER); } }); }
From source file:org.sfs.nodes.master.MasterNodeStopJob.java
License:Apache License
@Override public void handle(SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); Defer.aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsMasterNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(new ValidateHeaderExists(Jobs.Parameters.JOB_ID)) .map(new ValidateHeaderBetweenLong(Jobs.Parameters.TIMEOUT, 100, Long.MAX_VALUE)) .map(new ToVoid<>()).flatMap(aVoid -> { MultiMap headers = httpServerRequest.headers(); long timeout = headers.contains(Jobs.Parameters.TIMEOUT) ? Long.parseLong(headers.get(Jobs.Parameters.TIMEOUT)) : -1;//from ww w. j a v a 2 s . c o m String jobId = headers.get(Jobs.Parameters.JOB_ID); httpServerRequest.startProxyKeepAlive(); Jobs jobs = vertxContext.verticle().jobs(); return Defer.aVoid().flatMap(aVoid1 -> jobs.stop(vertxContext, jobId)).flatMap(aVoid1 -> { if (timeout >= 0) { return jobs.waitStopped(vertxContext, jobId, timeout, TimeUnit.MILLISECONDS); } else { return Defer.aVoid(); } }); }).single().subscribe(new Terminus<Void>(httpServerRequest) { @Override public void onNext(Void aVoid) { JsonObject responseJson = new JsonObject().put("code", HTTP_OK).put("message", "Success"); httpServerRequest.response().write(responseJson.encode(), StandardCharsets.UTF_8.toString()) .write(DELIMITER_BUFFER); } }); }
From source file:org.sfs.nodes.master.MasterNodeWaitForJob.java
License:Apache License
@Override public void handle(SfsRequest httpServerRequest) { VertxContext<Server> vertxContext = httpServerRequest.vertxContext(); Defer.aVoid().flatMap(new Authenticate(httpServerRequest)) .flatMap(new ValidateActionAdminOrSystem(httpServerRequest)) .map(new ValidateNodeIsMasterNode<>(vertxContext)).map(aVoid -> httpServerRequest) .map(new ValidateHeaderExists(Jobs.Parameters.JOB_ID)) .map(new ValidateHeaderBetweenLong(Jobs.Parameters.TIMEOUT, 100, Long.MAX_VALUE)) .map(new ToVoid<>()).flatMap(aVoid -> { MultiMap headers = httpServerRequest.headers(); long timeout = headers.contains(Jobs.Parameters.TIMEOUT) ? Long.parseLong(headers.get(Jobs.Parameters.TIMEOUT)) : -1;//ww w .j av a2 s.c om String jobId = headers.get(Jobs.Parameters.JOB_ID); httpServerRequest.startProxyKeepAlive(); Jobs jobs = vertxContext.verticle().jobs(); return Defer.aVoid().flatMap(aVoid1 -> { if (timeout >= 0) { return jobs.waitStopped(vertxContext, jobId, timeout, TimeUnit.MILLISECONDS); } else { return Defer.aVoid(); } }); }).single().subscribe(new Terminus<Void>(httpServerRequest) { @Override public void onNext(Void aVoid) { JsonObject responseJson = new JsonObject().put("code", HTTP_OK).put("message", "Success"); httpServerRequest.response().write(responseJson.encode(), StandardCharsets.UTF_8.toString()) .write(DELIMITER_BUFFER); } }); }
From source file:org.sfs.rx.ConnectionCloseTerminus.java
License:Apache License
protected void fixcyberduck() { SfsRequest serverRequest = getSfsRequest(); MultiMap headers = serverRequest.headers(); // cyberduck sends keep-alive but then gets screwed up when connection: close isn't sent // if this is not a proxied request and originated in cyberduck then send the connection: close // headers. If it is a proxied request let the proxy deal with the issue if (!headers.contains((X_FORWARDED_FOR))) { String userAgent = toLowerCase(headers.get(USER_AGENT)); if (userAgent != null && userAgent.contains("cyberduck")) { serverRequest.response().putHeader(CONNECTION, "close"); }/*from ww w . j a v a2s . c o m*/ } }