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.
/**
* Copyright 2011-2014 the original author or authors.
*/
package com.jetdrone.vertx.yoke.middleware;
import com.jetdrone.vertx.yoke.core.Context;
import com.jetdrone.vertx.yoke.core.YokeCookie;
import com.jetdrone.vertx.yoke.core.YokeFileUpload;
import com.jetdrone.vertx.yoke.store.SessionStore;
import org.jetbrains.annotations.NotNull;
import org.vertx.java.core.Handler;
import org.vertx.java.core.MultiMap;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.http.HttpServerFileUpload;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.core.http.HttpVersion;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.net.NetSocket;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.security.cert.X509Certificate;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.*;
/** YokeRequest is an extension to Vert.x *HttpServerRequest* with some helper methods to make it easier to perform common
* tasks related to web application development.
*/
public class YokeRequest implements HttpServerRequest {
private static final Comparator ACCEPT_X_COMPARATOR = new Comparator() {
float getQuality(String s) {
if (s == null) {
return 0;
}
String[] params = s.split(" *; *");
for (int i = 1; i < params.length; i++) {
String[] q = params[1].split(" *= *");
if ("q".equals(q[0])) {
return Float.parseFloat(q[1]);
}
}
return 1;
}
@Override
public int compare(String o1, String o2) {
float f1 = getQuality(o1);
float f2 = getQuality(o2);
if (f1 < f2) {
return 1;
}
if (f1 > f2) {
return -1;
}
return 0;
}
};
// the original request (if extensions need to access it, use the accessor)
final private HttpServerRequest request;
// the wrapped response (if extensions need to access it, use the accessor)
final private YokeResponse response;
// the request context
final protected Context context;
// is this request secure (if extensions need to access it, use the accessor)
final private boolean secure;
// session data store
final protected SessionStore store;
// we can overrride the setMethod
private String method;
private long bodyLengthLimit = -1;
// the body is protected so extensions can access the raw object instead of casted versions.
protected Object body;
private Map files;
private Set cookies;
// control flags
private boolean expectMultiPartCalled = false;
public YokeRequest(@NotNull final HttpServerRequest request, @NotNull final YokeResponse response, final boolean secure, @NotNull final Context context, @NotNull final SessionStore store) {
this.context = context;
this.request = request;
this.method = request.method();
response.setMethod(this.method);
this.response = response;
this.secure = secure;
this.store = store;
}
/** Allow getting properties in a generified way.
*
* @param name The key to get
* @return {R} The found object
*/
@SuppressWarnings("unchecked")
public R get(@NotNull final String name) {
// do some conversions for JsonObject/JsonArray
Object o = context.get(name);
if (o instanceof Map) {
return (R) new JsonObject((Map) o);
}
if (o instanceof List) {
return (R) new JsonArray((List) o);
}
return (R) o;
}
/** Allow getting properties in a generified way and return defaultValue if the key does not exist.
*
* @param name The key to get
* @param defaultValue value returned when the key does not exist
* @return {R} The found object
*/
public R get(@NotNull final String name, R defaultValue) {
if (context.containsKey(name)) {
return get(name);
} else {
return defaultValue;
}
}
/** Allows putting a value into the context
*
* @param name the key to store
* @param value the value to store
* @return {R} the previous value or null
*/
@SuppressWarnings("unchecked")
public R put(@NotNull final String name, R value) {
if (value == null) {
return (R) context.remove(name);
}
return (R) context.put(name, value);
}
/** Allow getting headers in a generified way.
*
* @param name The key to get
* @return The found object
*/
public String getHeader(@NotNull final String name) {
return headers().get(name);
}
/** Allow getting headers in a generified way.
*
* @param name The key to get
* @return {List} The list of all found objects
*/
public List getAllHeaders(@NotNull final String name) {
return headers().getAll(name);
}
/** Allow getting headers in a generified way and return defaultValue if the key does not exist.
*
* @param name The key to get
* @param defaultValue value returned when the key does not exist
* @return {String} The found object
*/
public String getHeader(@NotNull final String name, String defaultValue) {
if (headers().contains(name)) {
return getHeader(name);
} else {
return defaultValue;
}
}
/**
* Access all request cookies
* @return Set of cookies
*/
public Set cookies() {
return cookies;
}
/** Allow getting Cookie by name.
*
* @param name The key to get
* @return The found object
*/
public YokeCookie getCookie(@NotNull final String name) {
if (cookies != null) {
for (YokeCookie c : cookies) {
if (name.equals(c.getName())) {
return c;
}
}
}
return null;
}
/** Allow getting all Cookie by name.
*
* @param name The key to get
* @return The found objects
*/
public List getAllCookies(@NotNull final String name) {
List foundCookies = new ArrayList<>();
if (cookies != null) {
for (YokeCookie c : cookies) {
if (name.equals(c.getName())) {
foundCookies.add(c);
}
}
}
return foundCookies;
}
// The original HTTP setMethod for the request. One of GET, PUT, POST, DELETE, TRACE, CONNECT, OPTIONS or HEAD
public String originalMethod() {
return request.method();
}
/** Package level mutator for the overrided setMethod
* @param newMethod new setMethod GET, PUT, POST, DELETE, TRACE, CONNECT, OPTIONS or HEAD
*/
void setMethod(@NotNull final String newMethod) {
this.method = newMethod.toUpperCase();
response.setMethod(this.method);
}
// Package level mutator for the bodyLength
void setBodyLengthLimit(long limit) {
bodyLengthLimit = limit;
}
// Holds the maximum allowed length for the setBody data. -1 for unlimited
public long bodyLengthLimit() {
return bodyLengthLimit;
}
/** Returns true if this request has setBody
*
* @return {Boolean} true if content-length or transfer-encoding is present
*/
public boolean hasBody() {
MultiMap headers = headers();
return headers.contains("transfer-encoding") || headers.contains("content-length");
}
/** Returns the content length of this request setBody or -1 if header is not present.
*/
public long contentLength() {
String contentLengthHeader = headers().get("content-length");
if (contentLengthHeader != null) {
return Long.parseLong(contentLengthHeader);
} else {
return -1;
}
}
/** The request body and eventually a parsed version of it in json or map */
@SuppressWarnings("unchecked")
public V body() {
if (body != null) {
if (body instanceof Map) {
return (V) new JsonObject((Map) body);
}
if (body instanceof List) {
return (V) new JsonArray((List) body);
}
}
return (V) body;
}
/** Mutator for the request setBody
* The request setBody and eventually a parsed version of it in json or map*/
void setBody(Object body) {
this.body = body;
}
/** The uploaded setFiles */
public Map files() {
return files;
}
/** Get an uploaded file */
public YokeFileUpload getFile(@NotNull final String name) {
if (files == null) {
return null;
}
return files.get(name);
}
/** The uploaded setFiles */
void setFiles(Map files) {
this.files = files;
}
/** Cookies */
void setCookies(Set cookies) {
this.cookies = cookies;
}
// Session management
/** Destroys a session from the request context and also from the storage engine.
*/
public void destroySession() {
JsonObject session = get("session");
if (session == null) {
return;
}
String sessionId = session.getString("id");
// remove from the context
put("session", null);
if (sessionId == null) {
return;
}
store.destroy(sessionId, new Handler