/** * Adds the Session Id {@link HttpCookie} to the {@link HttpResponse}. * * @param sessionId * Session Id. * @param expireTime * Time that the {@link HttpCookie} is to expire. */ private void setSessionIdCookieToHttpResponse(String sessionId, Instant expireTime) { this.connection.getResponse().getCookies().setCookie(this.sessionIdCookieName, sessionId, (cookie) -> cookie.setExpires(expireTime)); }
/** * Enables extending implementations to configure themselves as the * {@link MockHttpServer}. * * @param httpServer {@link MockHttpServer}. * @param input {@link DeployedOfficeInput}. */ protected static void configureMockHttpServer(MockHttpServer httpServer, DeployedOfficeInput input) { new HttpServer(httpServer, httpServer, null, null, true, null, input, null, null); }
/** * Obtains the {@link SectionOutput} name for this route. * * @return {@link SectionOutput} name for this route. */ public String getOutputName() { return this.flowIndex + "_" + method.getName() + "_" + this.path; }
@Override public void handle(HttpArgument pathArguments, ServerHttpConnection connection, ManagedFunctionContext<?, Indexed> context) { // Determine if secure connection is required if (this.isRequireSecure && (!connection.isSecure())) { // Non-secure, requiring secure - so redirect String path = connection.getRequest().getUri(); String securePath = connection.getServerLocation().createClientUrl(this.isRequireSecure, path); // Send redirect response to secure path HttpResponse response = connection.getResponse(); response.setStatus(HttpStatus.TEMPORARY_REDIRECT); response.getHeaders().addHeader(LOCATION, securePath); return; } // Undertake flow to service route context.doFlow(this.flowIndex, pathArguments, null); }
@Override @SuppressWarnings("unchecked") public T parse(ServerHttpConnection connection) throws HttpException { try { return (T) mapper.readValue(connection.getRequest().getEntity(), javaType); } catch (IOException ex) { throw new HttpException(ex); } } };
@Override public Object execute(ManagedFunctionContext<HttpRouteDependencies, Indexed> context) { // Obtain the server HTTP connection ServerHttpConnection connection = (ServerHttpConnection) context .getObject(HttpRouteDependencies.SERVER_HTTP_CONNECTION); // Load the escalation handler if (this.escalationHandler != null) { connection.getResponse().setEscalationHandler(this.escalationHandler); } // Determine if potentially redirect HttpRequestCookie cookie = connection.getRequest().getCookies() .getCookie(HttpRedirectFunction.REDIRECT_COOKIE_NAME); if (cookie != null) { // Redirect, so trigger flow to import previous state context.doFlow(this.handleRedirectFlowIndex, cookie, null); return null; // serviced by redirect } // No redirect, so route the request return this.router.route(connection, context); }
@Override public void write(ServerWriter writer, boolean isDefaultCharset, Object bean, ServerHttpConnection connection, String templatePath) throws HttpException { // Obtain the link path (determining if require secure link) if (this.isLinkSecure && (!connection.isSecure())) { templatePath = connection.getServerLocation().createClientUrl(this.isLinkSecure, templatePath); } try { // Write the content writer.write(templatePath); writer.write(this.linkPathSuffix); } catch (IOException ex) { throw new HttpException(ex); } }
@Override public void send(T object, ServerHttpConnection connection) throws IOException { HttpResponse response = connection.getResponse(); response.setContentType(contentType, null); mapper.writeValue(response.getEntity(), object); } };
@Override public String extractApplicationPath(ServerHttpConnection connection) throws HttpException { return WebRouter.transformToApplicationCanonicalPath(connection.getRequest().getUri(), this.contextPath); }
@Override public String createApplicationClientUrl(boolean isSecure, String path, ServerHttpConnection connection) { // Create the application path path = this.createApplicationClientPath(path); // Determine if appropriately secure if (connection.isSecure() == isSecure) { // Relative path return path; } else { // Full path back to server return connection.getServerLocation().createClientUrl(isSecure, path); } }
@Override public boolean handle(HttpEscalationContext context) throws IOException { // Obtain the connection ServerHttpConnection connection = context.getServerHttpConnection(); // Obtain the acceptable content type ContentTypeCache contentTypeCache = this.escalationNegotiator.getHandler(connection.getRequest()); if (contentTypeCache == null) { return false; // not able to handle escalation } // Obtain the escalation Throwable escalation = context.getEscalation(); // Handle escalation handleObject(escalation, contentTypeCache, ESCALATION_RESPONDER_FACTORY, connection); return true; // handled }
@Override public void assertCookie(HttpResponseCookie cookie) { WritableHttpCookie writable = (WritableHttpCookie) cookie; String name = cookie.getName(); WritableHttpCookie actual = this.getCookie(name); Assert.assertNotNull("No cookie by name '" + name + "' for " + this.getRequestInfo(), actual); Assert.assertEquals("Incorrect cookie " + name + " for " + this.getRequestInfo(), writable.toResponseHeaderValue(), actual.toResponseHeaderValue()); }
/** * Creates a mock {@link HttpResponseCookie}. * * @param name Name. * @param value value. * @return {@link HttpResponseCookie}. */ public static WritableHttpCookie mockResponseCookie(String name, String value) { return new WritableHttpCookie(name, value, new MockProcessAwareContext()); }
@Override public void configureHttpServer(HttpServerImplementationContext context) { @SuppressWarnings("unchecked") Class<ProcessAwareServerHttpConnectionManagedObject<ByteBuffer>> managedObjectType = (Class<ProcessAwareServerHttpConnectionManagedObject<ByteBuffer>>) (Object) ProcessAwareServerHttpConnectionManagedObject.class; this.serviceInput = context.getExternalServiceInput(managedObjectType, ProcessAwareServerHttpConnectionManagedObject.getCleanupEscalationHandler()); }
@Override public String getContentType() { return contentType.getValue(); }
@Override public WritableHttpHeader getHeader(String name) { for (WritableHttpHeader header : this.headers) { if (name.equalsIgnoreCase(header.getName())) { return header; // found } } // As here, not found return null; }
@Override public void evaluate() throws Throwable { try (CloseableHttpClient client = HttpClientTestUtil.createHttpClient(HttpClientRule.this.isSecure)) { HttpClientRule.this.client = client; // Undertake test base.evaluate(); } finally { HttpClientRule.this.client = null; } } };
@Override public Object execute(ManagedFunctionContext<NotFoundDependencies, None> context) throws NotFoundHttpException { // Obtain the request path ServerHttpConnection connection = (ServerHttpConnection) context .getObject(NotFoundDependencies.SERVER_HTTP_CONNECTION); String requestPath = connection.getRequest().getUri(); // Not found throw new NotFoundHttpException(requestPath); }
@Override public String getContentType() { return contentType.getValue(); }
@Override public String getContentType() { return contentType.getValue(); }