use of io.micronaut.http.netty.cookies.NettyCookie in project micronaut-gcp by micronaut-projects.
the class GoogleFunctionHttpResponse method cookie.
@Override
public MutableHttpResponse<B> cookie(Cookie cookie) {
if (cookie instanceof NettyCookie) {
NettyCookie nettyCookie = (NettyCookie) cookie;
final String encoded = ServerCookieEncoder.STRICT.encode(nettyCookie.getNettyCookie());
header(HttpHeaders.SET_COOKIE, encoded);
}
return this;
}
use of io.micronaut.http.netty.cookies.NettyCookie in project micronaut-gcp by micronaut-projects.
the class HttpFunction method toGoogleRequest.
private HttpRequest toGoogleRequest(io.micronaut.http.HttpRequest<?> request) {
Map<String, List<String>> headers = new LinkedHashMap<>();
Map<String, List<String>> parameters = new LinkedHashMap<>();
request.getHeaders().forEach(headers::put);
request.getParameters().forEach(parameters::put);
Object body = request.getBody().orElse(null);
try {
Cookies cookies = request.getCookies();
cookies.forEach((s, cookie) -> {
if (cookie instanceof NettyCookie) {
headers.computeIfAbsent(HttpHeaders.COOKIE, s1 -> new ArrayList<>()).add(ClientCookieEncoder.STRICT.encode(((NettyCookie) cookie).getNettyCookie()));
}
});
} catch (UnsupportedOperationException e) {
// not all request types support retrieving cookies
}
return new HttpRequest() {
@Override
public String getMethod() {
return request.getMethodName();
}
@Override
public String getUri() {
return request.getUri().toString();
}
@Override
public String getPath() {
return request.getPath();
}
@Override
public Optional<String> getQuery() {
return Optional.of(request.getUri().getQuery());
}
@Override
public Map<String, List<String>> getQueryParameters() {
return parameters;
}
@Override
public Map<String, HttpPart> getParts() {
return Collections.emptyMap();
}
@Override
public Optional<String> getContentType() {
List<String> values = headers.get(HttpHeaders.CONTENT_TYPE);
if (values != null) {
Iterator<String> i = values.iterator();
if (i.hasNext()) {
return Optional.ofNullable(i.next());
}
}
return Optional.empty();
}
@Override
public long getContentLength() {
List<String> values = headers.get(HttpHeaders.CONTENT_LENGTH);
if (values != null) {
Iterator<String> i = values.iterator();
if (i.hasNext()) {
return Long.parseLong(i.next());
}
}
return 0;
}
@Override
public Optional<String> getCharacterEncoding() {
List<String> values = headers.get(HttpHeaders.CONTENT_ENCODING);
if (values != null) {
Iterator<String> i = values.iterator();
if (i.hasNext()) {
return Optional.ofNullable(i.next());
}
}
return Optional.empty();
}
@Override
public InputStream getInputStream() {
if (body != null) {
if (body instanceof byte[]) {
return new ByteArrayInputStream((byte[]) body);
} else {
MediaType mediaType = getContentType().map(MediaType::new).orElse(null);
if (mediaType != null) {
MediaTypeCodec codec = httpHandler.getMediaTypeCodecRegistry().findCodec(mediaType).orElse(null);
if (codec != null) {
byte[] bytes = codec.encode(body);
return new ByteArrayInputStream(bytes);
}
} else {
return new ByteArrayInputStream(body.toString().getBytes(StandardCharsets.UTF_8));
}
}
}
return new ByteArrayInputStream(new byte[0]);
}
@Override
public BufferedReader getReader() {
return new BufferedReader(new InputStreamReader(getInputStream(), StandardCharsets.UTF_8));
}
@Override
public Map<String, List<String>> getHeaders() {
return headers;
}
};
}
Aggregations