Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The MIT License
*
* Copyright 2020 Intuit Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.intuit.karate.http;
import com.intuit.karate.resource.ResourceResolver;
import com.intuit.karate.FileUtils;
import com.intuit.karate.JsonUtils;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.handler.codec.http.cookie.DefaultCookie;
import io.netty.handler.codec.http.cookie.ServerCookieEncoder;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pthomas3
*/
public class ResponseBuilder {
private static final Logger logger = LoggerFactory.getLogger(ResponseBuilder.class);
private byte[] body;
private Set cookies;
private Map> headers;
private ResourceType resourceType;
private final ServerConfig config;
private final ResourceResolver resourceResolver;
public ResponseBuilder(ServerConfig config, RequestCycle rc) {
this.config = config;
resourceResolver = config.getResourceResolver();
if (rc != null) {
Response response = rc.getResponse();
headers = response.getHeaders();
}
}
public ResponseBuilder body(String body) {
this.body = FileUtils.toBytes(body);
return this;
}
public ResponseBuilder html(String body) {
body(body);
contentTypeHtml();
return this;
}
public ResponseBuilder body(InputStream body) {
this.body = FileUtils.toBytes(body);
return this;
}
public ResponseBuilder locationHeader(String url) {
return header(HttpConstants.HDR_LOCATION, url);
}
public ResponseBuilder contentTypeHtml() {
resourceType = ResourceType.HTML;
contentType(resourceType.contentType);
return this;
}
public ResponseBuilder contentType(String contentType) {
if (contentType != null) {
header(HttpConstants.HDR_CONTENT_TYPE, contentType);
}
return this;
}
public ResponseBuilder cookie(String name, String value) {
return cookie(name, value, false);
}
public ResponseBuilder sessionCookie(String value) {
return cookie(config.getSessionCookieName(), value);
}
public ResponseBuilder deleteSessionCookie(String value) {
return cookie(config.getSessionCookieName(), value, true);
}
private ResponseBuilder cookie(String name, String value, boolean delete) {
DefaultCookie cookie = new DefaultCookie(name, value);
if (delete) {
cookie.setMaxAge(0);
}
if (cookies == null) {
cookies = new HashSet();
}
cookies.add(cookie);
return this;
}
public ResponseBuilder header(String name, String value) {
if (headers == null) {
headers = new LinkedHashMap();
}
headers.put(name, Collections.singletonList(value));
return this;
}
public ResponseBuilder ajaxRedirect(String url) {
header(HttpConstants.HDR_HX_REDIRECT, url);
return this;
}
public ResponseBuilder trigger(String json) {
header(HttpConstants.HDR_HX_TRIGGER, JsonUtils.toStrictJson(json));
return this;
}
public ResponseBuilder session(Session session, boolean newSession) {
if (session != null && newSession) {
sessionCookie(session.getId());
}
return this;
}
public Response build(RequestCycle rc) {
Response response = rc.getResponse();
ServerContext context = rc.getContext();
List