private void failureHandler(RoutingContext context) { LOGGER.error("http server failed.", context.failure()); AbstractRestInvocation restProducerInvocation = context.get(RestConst.REST_PRODUCER_INVOCATION); Throwable e = context.failure(); if (ErrorDataDecoderException.class.isInstance(e)) { Throwable cause = e.getCause(); if (InvocationException.class.isInstance(cause)) { e = cause; } } restProducerInvocation.sendFailResponse(e); // 走到这里,应该都是不可控制的异常,直接关闭连接 context.response().close(); }
@Test public void failureHandlerErrorDataWithInvocation(@Mocked RoutingContext context, @Mocked InvocationException e) { RestProducerInvocation restProducerInvocation = new RestProducerInvocation(); ErrorDataDecoderException edde = new ErrorDataDecoderException(e); new Expectations() { { context.get(RestConst.REST_PRODUCER_INVOCATION); result = restProducerInvocation; context.failure(); returns(edde, edde); } }; Deencapsulation.invoke(dispatcher, "failureHandler", context); Assert.assertSame(e, this.throwable); }
@Test public void failureHandlerErrorDataWithNormal(@Mocked RoutingContext context) { RestProducerInvocation restProducerInvocation = new RestProducerInvocation(); Exception e = new Exception(); ErrorDataDecoderException edde = new ErrorDataDecoderException(e); new Expectations() { { context.get(RestConst.REST_PRODUCER_INVOCATION); result = restProducerInvocation; context.failure(); returns(edde, edde); } }; Deencapsulation.invoke(dispatcher, "failureHandler", context); Assert.assertSame(edde, this.throwable); }
BHandler(RoutingContext context) { this.context = context; Set<FileUpload> fileUploads = context.fileUploads(); final String contentType = context.request().getHeader(HttpHeaders.CONTENT_TYPE); if (contentType == null) { isMultipart = false; isUrlEncoded = false; } else { final String lowerCaseContentType = contentType.toLowerCase(); isMultipart = lowerCaseContentType.startsWith(HttpHeaderValues.MULTIPART_FORM_DATA.toString()); isUrlEncoded = lowerCaseContentType.startsWith(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString()); } if (isMultipart || isUrlEncoded) { makeUploadDir(context.vertx().fileSystem()); context.request().setExpectMultipart(true); context.request().uploadHandler(upload -> { // *** cse begin *** if (uploadsDir == null) { failed = true; CommonExceptionData data = new CommonExceptionData("not support file upload."); throw new ErrorDataDecoderException(ExceptionFactory.createConsumerException(data)); } // *** cse end *** // we actually upload to a file with a generated filename uploadCount.incrementAndGet(); String uploadedFileName = new File(uploadsDir, UUID.randomUUID().toString()).getPath(); upload.streamToFileSystem(uploadedFileName); FileUploadImpl fileUpload = new FileUploadImpl(uploadedFileName, upload); fileUploads.add(fileUpload); upload.exceptionHandler(context::fail); upload.endHandler(v -> uploadEnded()); }); } context.request().exceptionHandler(context::fail); }
/** * Decode component * * @return the decoded component */ private static String decodeAttribute(String s, Charset charset) { try { return QueryStringDecoder.decodeComponent(s, charset); } catch (IllegalArgumentException e) { throw new ErrorDataDecoderException("Bad string: '" + s + '\'', e); } }
/** * Find the next Multipart Delimiter * * @param delimiter * delimiter to find * @param dispositionStatus * the next getStatus if the delimiter is a start * @param closeDelimiterStatus * the next getStatus if the delimiter is a close delimiter * @return the next InterfaceHttpData if any * @throws ErrorDataDecoderException */ private InterfaceHttpData findMultipartDelimiter(String delimiter, MultiPartStatus dispositionStatus, MultiPartStatus closeDelimiterStatus) { // --AaB03x or --AaB03x-- int readerIndex = undecodedChunk.readerIndex(); try { skipControlCharacters(); } catch (NotEnoughDataDecoderException e1) { undecodedChunk.readerIndex(readerIndex); return null; } skipOneLine(); String newline; try { newline = readDelimiter(delimiter); } catch (NotEnoughDataDecoderException e) { undecodedChunk.readerIndex(readerIndex); return null; } if (newline.equals(delimiter)) { currentStatus = dispositionStatus; return decodeMultipart(dispositionStatus); } if (newline.equals(delimiter + "--")) { // CLOSEDELIMITER or MIXED CLOSEDELIMITER found currentStatus = closeDelimiterStatus; if (currentStatus == MultiPartStatus.HEADERDELIMITER) { // MIXEDCLOSEDELIMITER // end of the Mixed part currentFieldAttributes = null; return decodeMultipart(MultiPartStatus.HEADERDELIMITER); } return null; } undecodedChunk.readerIndex(readerIndex); throw new ErrorDataDecoderException("No Multipart delimiter found"); }
@Test public void shouldHandleErrorDataDecoderException() { verifyExceptionHandled(new ErrorDataDecoderException(), singletonError(testProjectApiErrors.getMalformedRequestApiError())); }