本文整理汇总了Java中enkan.util.HttpResponseUtils类的典型用法代码示例。如果您正苦于以下问题:Java HttpResponseUtils类的具体用法?Java HttpResponseUtils怎么用?Java HttpResponseUtils使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpResponseUtils类属于enkan.util包,在下文中一共展示了HttpResponseUtils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: responseSignedIn
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
public HttpResponse responseSignedIn(String token, HttpRequest request, String redirectUrl) {
Cookie tokenCookie = Cookie.create(config.getTokenName(), token);
tokenCookie.setPath("/");
tokenCookie.setHttpOnly(true);
if (Objects.equals(request.getHeaders().get("X-Requested-With"), "XMLHttpRequest")) {
Multimap<String, Cookie> cookies = Multimap.of(tokenCookie.getName(), tokenCookie);
return builder(HttpResponse.of("{\"url\":\"" + Optional.ofNullable(redirectUrl).orElse("/my") + "\"}"))
.set(HttpResponse::setHeaders, Headers.of("Content-Type", "application/json"))
.set(HttpResponse::setCookies, cookies)
.build();
} else {
return builder(redirect(Optional.ofNullable(redirectUrl).orElse("/my"),
HttpResponseUtils.RedirectStatusCode.SEE_OTHER))
.set(HttpResponse::setCookies, Multimap.of(tokenCookie.getName(), tokenCookie))
.build();
}
}
示例2: handle
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Override
public HttpResponse handle(HttpRequest request, MiddlewareChain next) {
HttpResponse response = castToHttpResponse(next.next(request));
if (response != null && (isEmptyBody(response) || isMoreCats(response))) {
String type = getHeader(response, "Content-Type");
if (type == null || "text/html".equals(type)) {
HttpResponseUtils.header(response, "Content-Type", "text/html");
Context context = new Context();
context.setVariable("status", response.getStatus());
StringWriter sw = new StringWriter();
template.render(context, sw);
response.setBody(sw.toString());
}
}
return response;
}
示例3: render
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Override
public HttpResponse render(String name, Object... keyOrVals) {
TemplatedHttpResponse response = TemplatedHttpResponse.create(name, keyOrVals);
response.setBody(new LazyRenderInputStream(() -> {
// FIXME set locale.
Context ctx = new Context(Locale.US, response.getContext());
try {
return new ByteArrayInputStream(thymeleafEngine.process(name, ctx).getBytes(encoding));
} catch (UnsupportedEncodingException e) {
throw new MisconfigurationException("core.UNSUPPORTED_ENCODING", encoding, e);
}
}));
HttpResponseUtils.contentType(response, "text/html");
return response;
}
示例4: authorize
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
/**
* Authorization endpoint.
*
* @param params Request parameters
* @param principal User principal
* @param request HttpRequest object
* @return Authorization response
*/
public HttpResponse authorize(Parameters params, UserPermissionPrincipal principal, HttpRequest request) {
if (principal != null) {
Parameters responseParams = Parameters.of();
if (params.containsKey("state")) {
responseParams.put("state", params.get("state"));
}
String clientId = params.get("client_id");
OidcApplicationDao oidcApplicationDao = daoProvider.getDao(OidcApplicationDao.class);
OidcApplication oidcApplication = oidcApplicationDao.selectByClientId(clientId);
String redirectUrl = (String) params.getOrDefault("redirect_url", oidcApplication.getCallbackUrl());
Set<ResponseType> responseTypes = Arrays.stream(((String) params.getOrDefault("response_type", "code")).split("[ ,]+"))
.map(rt -> ResponseType.of(rt))
.filter(Objects::nonNull)
.collect(Collectors.toSet());
if (responseTypes.contains(ID_TOKEN)) {
String nonce = params.get("nonce");
responseParams.put("id_token", createIdToken(principal.getId(), oidcApplication, nonce));
}
if (responseTypes.contains(TOKEN)) {
responseParams.put("access_token", createAccessToken(principal.getName(), clientId, Collections.emptyList()));
responseParams.put("token_type", "bearer");
responseParams.put("expires_in", 3600);
}
if (responseTypes.contains(CODE)) {
KeyValueStore authorizationCodeStore = storeProvider.getStore(AUTHORIZATION_CODE);
String code = RandomUtils.generateRandomString(16, config.getSecureRandom());
authorizationCodeStore.write(code, principal.getId());
responseParams.put("code", code);
}
return HttpResponseUtils.redirect(makeCallbackUrl(redirectUrl, responseParams, responseTypes), FOUND);
} else {
return HttpResponseUtils.redirect("/my/signIn?url=" + request.getUri() + "?" + CodecUtils.urlEncode(request.getQueryString()), SEE_OTHER);
}
}
示例5: render
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
protected HttpResponse<String> render(Template template, Object... args) {
StringWriter sw = new StringWriter();
Context ctx = new Context();
for (int i = 0; i < args.length; i += 2) {
ctx.setVariable(Objects.toString(args[i], ""), args[i+1]);
}
template.render(ctx, sw);
HttpResponse<String> response = HttpResponse.of(sw.toString());
HttpResponseUtils.contentType(response, "text/html");
return response;
}
示例6: login
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
public HttpResponse login(Parameters params, Conversation conversation) {
if (!conversation.isTransient()) conversation.end();
CustomerDao dao = daoProvider.getDao(CustomerDao.class);
String email = params.get("email");
Customer customer = dao.loginByPassword(email, params.get("password"));
if (customer == null) {
return templateEngine.render("guestbook/login");
} else {
Session session = new Session();
session.put("principal", new LoginPrincipal(email));
return builder(redirect(GuestbookController.class, "list", HttpResponseUtils.RedirectStatusCode.SEE_OTHER))
.set(HttpResponse::setSession, session)
.build();
}
}
示例7: addCharset
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
protected void addCharset(HttpResponse response, String charset) {
String contentType = getHeader(response, "Content-Type");
if (isTextBasedContentType(contentType)
&& !isContainsCharset(contentType)) {
HttpResponseUtils.charset(response, charset);
}
}
示例8: handle
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Override
public HttpResponse handle(HttpRequest request, MiddlewareChain chain) {
String headerValue = formatFrameOptions();
HttpResponse response = castToHttpResponse(chain.next(request));
HttpResponseUtils.header(response, "X-Frame-Options", headerValue);
return response;
}
示例9: contentTypeResponse
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
protected void contentTypeResponse(HttpResponse response, HttpRequest request) {
if (HttpResponseUtils.getHeader(response, "Content-Type") == null) {
String uri = request.getUri();
String type = MimeTypeUtils.extMimeType(uri);
if (type == null) {
type = response.getBody() instanceof String ? "text/plain" : "application/octet-stream";
}
HttpResponseUtils.contentType(response, type);
}
}
示例10: testSetContentType
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Test
public void testSetContentType() {
MiddlewareChain<HttpRequest, HttpResponse> chain = new DefaultMiddlewareChain(new AnyPredicate(), null,
(Endpoint<HttpRequest, HttpResponse>) req ->
builder(HttpResponse.of("hello"))
.set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/html"))
.build());
HttpResponse response = middleware.handle(request, chain);
assertEquals("text/html", HttpResponseUtils.getHeader(response, "content-type"));
}
示例11: defaultValueIsOctetStream
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Test
public void defaultValueIsOctetStream() {
MiddlewareChain<HttpRequest, HttpResponse> chain = new DefaultMiddlewareChain(new AnyPredicate(), null,
(Endpoint<HttpRequest, HttpResponse>) req ->
builder(HttpResponse.of("hello"))
.build());
HttpResponse response = middleware.handle(request, chain);
assertEquals("text/plain", HttpResponseUtils.getHeader(response, "content-type"));
}
示例12: testRedirect
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Test
public void testRedirect() {
MiddlewareChain<HttpRequest, HttpResponse> chain = new DefaultMiddlewareChain(new AnyPredicate(), null,
(Endpoint<HttpRequest, HttpResponse>) req ->
builder(HttpResponse.of("hello"))
.set(HttpResponse::setStatus, 303)
.set(HttpResponse::setHeaders, Headers.of("Location", "foo/bar"))
.build());
HttpResponse response = middleware.handle(request, chain);
assertEquals("This is a specification of ring content_type middleware",
"text/plain", HttpResponseUtils.getHeader(response, "content-type"));
}
示例13: addXssBlockHeader
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Test
public void addXssBlockHeader() {
XssProtectionMiddleware middleware = new XssProtectionMiddleware();
HttpRequest request = builder(new DefaultHttpRequest())
.build();
MiddlewareChain<HttpRequest, HttpResponse> chain = new DefaultMiddlewareChain(new AnyPredicate(), null,
(Endpoint<HttpRequest, HttpResponse>) req -> builder(HttpResponse.of("hello")).build());
HttpResponse response = middleware.handle(request, chain);
assertEquals("1; mode=block", HttpResponseUtils.getHeader(response, "X-XSS-Protection"));
}
示例14: test
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Test
public void test() {
DefaultCharsetMiddleware middleware = new DefaultCharsetMiddleware();
HttpResponse response = builder(HttpResponse.of("aaa"))
.set(HttpResponse::setHeaders, Headers.of("Content-Type", "text/html"))
.build();
assertNotNull(HttpResponseUtils.getHeader(response, "Content-Type"));
middleware.addCharset(response, "UTF-8");
String type = HttpResponseUtils.getHeader(response, "Content-Type");
assertNotNull(type);
assertTrue(type.contains("UTF-8"));
}
示例15: handle
import enkan.util.HttpResponseUtils; //导入依赖的package包/类
@Override
public HttpResponse handle(HttpRequest request, MiddlewareChain chain) {
if (throttles.stream()
.anyMatch(throttle -> throttle.apply(request))) {
return builder(HttpResponseUtils.response("Too Many Request"))
.set(HttpResponse::setStatus, 429)
.build();
}
return castToHttpResponse(chain.next(request));
}