Browse Source

Provide current request/response through VaadinRequest/VaadinResponse (#9642)

This greatly improves discoverability of the feature and was made possible
by dropping Java 7 support
tags/8.1.0.rc2
Artur 6 years ago
parent
commit
983a572c8f

+ 15
- 0
server/src/main/java/com/vaadin/server/VaadinRequest.java View File

import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;


import com.vaadin.util.CurrentInstance;

/** /**
* A generic request to the server, wrapping a more specific request type, e.g. * A generic request to the server, wrapping a more specific request type, e.g.
* HttpServletReqest or PortletRequest. * HttpServletReqest or PortletRequest.
*/ */
public Enumeration<String> getHeaders(String name); public Enumeration<String> getHeaders(String name);


/**
* Gets the currently processed Vaadin request. The current request is
* automatically defined when the request is started. The current request
* can not be used in e.g. background threads because of the way server
* implementations reuse request instances.
*
* @return the current Vaadin request instance if available, otherwise
* <code>null</code>
* @since 8.1
*/
public static VaadinRequest getCurrent() {
return CurrentInstance.get(VaadinRequest.class);
}
} }

+ 16
- 0
server/src/main/java/com/vaadin/server/VaadinResponse.java View File

import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;


import com.vaadin.util.CurrentInstance;

/** /**
* A generic response from the server, wrapping a more specific response type, * A generic response from the server, wrapping a more specific response type,
* e.g. HttpServletResponse or PortletResponse. * e.g. HttpServletResponse or PortletResponse.
* @since 7.3.8 * @since 7.3.8
*/ */
public void setContentLength(int len); public void setContentLength(int len);

/**
* Gets the currently processed Vaadin response. The current response is
* automatically defined when the request is started. The current response
* can not be used in e.g. background threads because of the way server
* implementations reuse response instances.
*
* @return the current Vaadin response instance if available, otherwise
* <code>null</code>
* @since 8.1
*/
public static VaadinResponse getCurrent() {
return CurrentInstance.get(VaadinResponse.class);
}
} }

+ 10
- 8
server/src/main/java/com/vaadin/server/VaadinService.java View File

* @see #setCurrentInstances(VaadinRequest, VaadinResponse) * @see #setCurrentInstances(VaadinRequest, VaadinResponse)
*/ */
public static VaadinRequest getCurrentRequest() { public static VaadinRequest getCurrentRequest() {
return CurrentInstance.get(VaadinRequest.class);
return VaadinRequest.getCurrent();
} }


/** /**
* @see #setCurrentInstances(VaadinRequest, VaadinResponse) * @see #setCurrentInstances(VaadinRequest, VaadinResponse)
*/ */
public static VaadinResponse getCurrentResponse() { public static VaadinResponse getCurrentResponse() {
return CurrentInstance.get(VaadinResponse.class);
return VaadinResponse.getCurrent();
} }


/** /**
*/ */
private int getUidlRequestTimeout(VaadinSession session) { private int getUidlRequestTimeout(VaadinSession session) {
return getDeploymentConfiguration().isCloseIdleSessions() return getDeploymentConfiguration().isCloseIdleSessions()
? session.getSession().getMaxInactiveInterval() : -1;
? session.getSession().getMaxInactiveInterval()
: -1;
} }


/** /**
/** /**
* Returns whether the given session is active or whether it can be closed. * Returns whether the given session is active or whether it can be closed.
* <p> * <p>
* A session is active if and only if its {@link VaadinSession#getState()} returns {@link State#OPEN}
* and {@link #getUidlRequestTimeout(VaadinSession) getUidlRequestTimeout}
* is negative or has not yet expired.
* A session is active if and only if its {@link VaadinSession#getState()}
* returns {@link State#OPEN} and
* {@link #getUidlRequestTimeout(VaadinSession) getUidlRequestTimeout} is
* negative or has not yet expired.
* *
* @param session * @param session
* The session whose status to check * The session whose status to check
* <p> * <p>
* The framework collects filters from the {@link SessionInitEvent} where * The framework collects filters from the {@link SessionInitEvent} where
* session init listeners can add them. This method is called with the * session init listeners can add them. This method is called with the
* combined list to optionally modify it, and the result is then stored
* by the caller as the final list to use.
* combined list to optionally modify it, and the result is then stored by
* the caller as the final list to use.
* <p> * <p>
* The filters are called in the order the session init listeners are * The filters are called in the order the session init listeners are
* called, which is undefined. If you need a specific order, you can * called, which is undefined. If you need a specific order, you can

+ 21
- 0
server/src/main/java/com/vaadin/server/VaadinServletRequest.java View File

public VaadinServletService getService() { public VaadinServletService getService() {
return vaadinService; return vaadinService;
} }

/**
* Gets the currently processed Vaadin servlet request. The current request
* is automatically defined when the request is started. The current request
* can not be used in e.g. background threads because of the way server
* implementations reuse request instances.
*
*
* @return the current Vaadin servlet request instance if available,
* otherwise <code>null</code>
* @since 8.1
*/
public static VaadinServletRequest getCurrent() {
VaadinRequest currentRequest = VaadinRequest.getCurrent();
if (currentRequest instanceof VaadinServletRequest) {
return (VaadinServletRequest) currentRequest;
} else {
return null;
}
}

} }

+ 20
- 0
server/src/main/java/com/vaadin/server/VaadinServletResponse.java View File

public VaadinServletService getService() { public VaadinServletService getService() {
return vaadinService; return vaadinService;
} }

/**
* Gets the currently processed Vaadin servlet response. The current
* response is automatically defined when the request is started. The
* current response can not be used in e.g. background threads because of
* the way server implementations reuse response instances.
*
* @return the current Vaadin servlet response instance if available,
* otherwise <code>null</code>
* @since 8.1
*/
public static VaadinServletResponse getCurrent() {
VaadinResponse currentResponse = VaadinResponse.getCurrent();
if (currentResponse instanceof VaadinServletResponse) {
return (VaadinServletResponse) currentResponse;
} else {
return null;
}
}

} }

+ 2
- 7
server/src/main/java/com/vaadin/server/VaadinServletService.java View File

} }


public static HttpServletRequest getCurrentServletRequest() { public static HttpServletRequest getCurrentServletRequest() {
VaadinRequest currentRequest = VaadinService.getCurrentRequest();
if (currentRequest instanceof VaadinServletRequest) {
return (VaadinServletRequest) currentRequest;
} else {
return null;
}
return VaadinServletRequest.getCurrent();
} }


public static VaadinServletResponse getCurrentResponse() { public static VaadinServletResponse getCurrentResponse() {
return (VaadinServletResponse) VaadinService.getCurrentResponse();
return VaadinServletResponse.getCurrent();
} }


@Override @Override

Loading…
Cancel
Save