org.springframework.web.HttpSessionRequiredException Java Examples

The following examples show how to use org.springframework.web.HttpSessionRequiredException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Populate the model in the following order:
 * <ol>
 * <li>Retrieve "known" session attributes listed as {@code @SessionAttributes}.
 * <li>Invoke {@code @ModelAttribute} methods
 * <li>Find {@code @ModelAttribute} method arguments also listed as
 * {@code @SessionAttributes} and ensure they're present in the model raising
 * an exception if necessary.
 * </ol>
 * @param request the current request
 * @param container a container with the model to be initialized
 * @param handlerMethod the method for which the model is initialized
 * @throws Exception may arise from {@code @ModelAttribute} methods
 */
public void initModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod)
		throws Exception {

	Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request);
	container.mergeAttributes(sessionAttributes);
	invokeModelAttributeMethods(request, container);

	for (String name : findSessionAttributeArguments(handlerMethod)) {
		if (!container.containsAttribute(name)) {
			Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
			if (value == null) {
				throw new HttpSessionRequiredException("Expected session attribute '" + name + "'", name);
			}
			container.addAttribute(name, value);
		}
	}
}
 
Example #2
Source File: ModelFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void sessionAttributeNotPresent() throws Exception {
	ModelFactory modelFactory = new ModelFactory(null, null, this.attributeHandler);
	HandlerMethod handlerMethod = createHandlerMethod("handleSessionAttr", String.class);
	try {
		modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod);
		fail("Expected HttpSessionRequiredException");
	}
	catch (HttpSessionRequiredException ex) {
		// expected
	}

	// Now add attribute and try again
	this.attributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue");

	modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod);
	assertEquals("sessionAttrValue", this.mavContainer.getModel().get("sessionAttr"));
}
 
Example #3
Source File: ModelFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Populate the model in the following order:
 * <ol>
 * <li>Retrieve "known" session attributes listed as {@code @SessionAttributes}.
 * <li>Invoke {@code @ModelAttribute} methods
 * <li>Find {@code @ModelAttribute} method arguments also listed as
 * {@code @SessionAttributes} and ensure they're present in the model raising
 * an exception if necessary.
 * </ol>
 * @param request the current request
 * @param container a container with the model to be initialized
 * @param handlerMethod the method for which the model is initialized
 * @throws Exception may arise from {@code @ModelAttribute} methods
 */
public void initModel(NativeWebRequest request, ModelAndViewContainer container, HandlerMethod handlerMethod)
		throws Exception {

	Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request);
	container.mergeAttributes(sessionAttributes);
	invokeModelAttributeMethods(request, container);

	for (String name : findSessionAttributeArguments(handlerMethod)) {
		if (!container.containsAttribute(name)) {
			Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
			if (value == null) {
				throw new HttpSessionRequiredException("Expected session attribute '" + name + "'", name);
			}
			container.addAttribute(name, value);
		}
	}
}
 
Example #4
Source File: ModelFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void sessionAttributeNotPresent() throws Exception {
	ModelFactory modelFactory = new ModelFactory(null, null, this.attributeHandler);
	HandlerMethod handlerMethod = createHandlerMethod("handleSessionAttr", String.class);
	try {
		modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod);
		fail("Expected HttpSessionRequiredException");
	}
	catch (HttpSessionRequiredException ex) {
		// expected
	}

	// Now add attribute and try again
	this.attributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue");

	modelFactory.initModel(this.webRequest, this.mavContainer, handlerMethod);
	assertEquals("sessionAttrValue", this.mavContainer.getModel().get("sessionAttr"));
}
 
Example #5
Source File: ModelFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Populate the model in the following order:
 * <ol>
 * <li>Retrieve "known" session attributes listed as {@code @SessionAttributes}.
 * <li>Invoke {@code @ModelAttribute} methods
 * <li>Find {@code @ModelAttribute} method arguments also listed as
 * {@code @SessionAttributes} and ensure they're present in the model raising
 * an exception if necessary.
 * </ol>
 * @param request the current request
 * @param container a container with the model to be initialized
 * @param handlerMethod the method for which the model is initialized
 * @throws Exception may arise from {@code @ModelAttribute} methods
 */
public void initModel(NativeWebRequest request, ModelAndViewContainer container,
		HandlerMethod handlerMethod) throws Exception {

	Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request);
	container.mergeAttributes(sessionAttributes);
	invokeModelAttributeMethods(request, container);

	for (String name : findSessionAttributeArguments(handlerMethod)) {
		if (!container.containsAttribute(name)) {
			Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
			if (value == null) {
				throw new HttpSessionRequiredException("Expected session attribute '" + name + "'", name);
			}
			container.addAttribute(name, value);
		}
	}
}
 
Example #6
Source File: ModelFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Populate the model in the following order:
 * <ol>
 * 	<li>Retrieve "known" session attributes listed as {@code @SessionAttributes}.
 * 	<li>Invoke {@code @ModelAttribute} methods
 * 	<li>Find {@code @ModelAttribute} method arguments also listed as
 * 	{@code @SessionAttributes} and ensure they're present in the model raising
 * 	an exception if necessary.
 * </ol>
 * @param request the current request
 * @param mavContainer a container with the model to be initialized
 * @param handlerMethod the method for which the model is initialized
 * @throws Exception may arise from {@code @ModelAttribute} methods
 */
public void initModel(NativeWebRequest request, ModelAndViewContainer mavContainer, HandlerMethod handlerMethod)
		throws Exception {

	Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request);
	mavContainer.mergeAttributes(sessionAttributes);

	invokeModelAttributeMethods(request, mavContainer);

	for (String name : findSessionAttributeArguments(handlerMethod)) {
		if (!mavContainer.containsAttribute(name)) {
			Object value = this.sessionAttributesHandler.retrieveAttribute(request, name);
			if (value == null) {
				throw new HttpSessionRequiredException("Expected session attribute '" + name + "'");
			}
			mavContainer.addAttribute(name, value);
		}
	}
}
 
Example #7
Source File: ModelFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void sessionAttributeNotPresent() throws Exception {
	ModelFactory modelFactory = new ModelFactory(null, null, this.sessionAttrsHandler);

	try {
		modelFactory.initModel(this.webRequest, new ModelAndViewContainer(), this.handleSessionAttrMethod);
		fail("Expected HttpSessionRequiredException");
	}
	catch (HttpSessionRequiredException e) {
		// expected
	}

	this.sessionAttributeStore.storeAttribute(this.webRequest, "sessionAttr", "sessionAttrValue");
	ModelAndViewContainer mavContainer = new ModelAndViewContainer();
	modelFactory.initModel(this.webRequest, mavContainer, this.handleSessionAttrMethod);

	assertEquals("sessionAttrValue", mavContainer.getModel().get("sessionAttr"));
}
 
Example #8
Source File: WebContentGenerator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Check the given request for supported methods and a required session, if any.
 * @param request current HTTP request
 * @throws ServletException if the request cannot be handled because a check failed
 * @since 4.2
 */
protected final void checkRequest(HttpServletRequest request) throws ServletException {
	// Check whether we should support the request method.
	String method = request.getMethod();
	if (this.supportedMethods != null && !this.supportedMethods.contains(method)) {
		throw new HttpRequestMethodNotSupportedException(method, this.supportedMethods);
	}

	// Check whether a session is required.
	if (this.requireSession && request.getSession(false) == null) {
		throw new HttpSessionRequiredException("Pre-existing session required but none found");
	}
}
 
Example #9
Source File: WebContentGenerator.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check the given request for supported methods and a required session, if any.
 * @param request current HTTP request
 * @throws ServletException if the request cannot be handled because a check failed
 * @since 4.2
 */
protected final void checkRequest(HttpServletRequest request) throws ServletException {
	// Check whether we should support the request method.
	String method = request.getMethod();
	if (this.supportedMethods != null && !this.supportedMethods.contains(method)) {
		throw new HttpRequestMethodNotSupportedException(method, this.supportedMethods);
	}

	// Check whether a session is required.
	if (this.requireSession && request.getSession(false) == null) {
		throw new HttpSessionRequiredException("Pre-existing session required but none found");
	}
}
 
Example #10
Source File: WebContentGenerator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check the given request for supported methods and a required session, if any.
 * @param request current HTTP request
 * @throws ServletException if the request cannot be handled because a check failed
 * @since 4.2
 */
protected final void checkRequest(HttpServletRequest request) throws ServletException {
	// Check whether we should support the request method.
	String method = request.getMethod();
	if (this.supportedMethods != null && !this.supportedMethods.contains(method)) {
		throw new HttpRequestMethodNotSupportedException(method, this.supportedMethods);
	}

	// Check whether a session is required.
	if (this.requireSession && request.getSession(false) == null) {
		throw new HttpSessionRequiredException("Pre-existing session required but none found");
	}
}
 
Example #11
Source File: WebContentGenerator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Check the given request for supported methods and a required session, if any.
 * @param request current HTTP request
 * @throws ServletException if the request cannot be handled because a check failed
 * @since 4.2
 */
protected final void checkRequest(HttpServletRequest request) throws ServletException {
	// Check whether we should support the request method.
	String method = request.getMethod();
	if (this.supportedMethods != null && !this.supportedMethods.contains(method)) {
		throw new HttpRequestMethodNotSupportedException(
				method, StringUtils.toStringArray(this.supportedMethods));
	}

	// Check whether a session is required.
	if (this.requireSession && request.getSession(false) == null) {
		throw new HttpSessionRequiredException("Pre-existing session required but none found");
	}
}
 
Example #12
Source File: AnnotationMethodHandlerAdapter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void raiseSessionRequiredException(String message) throws Exception {
	throw new HttpSessionRequiredException(message);
}
 
Example #13
Source File: AnnotationMethodHandlerAdapter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected void raiseSessionRequiredException(String message) throws Exception {
	throw new HttpSessionRequiredException(message);
}
 
Example #14
Source File: MultiActionControllerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public ModelAndView handleServletException(HttpServletRequest request, HttpServletResponse response,
		HttpSessionRequiredException ex) {
	this.invoked.put("handle(SRE)", Boolean.TRUE);
	return new ModelAndView("handle(SRE)");
}
 
Example #15
Source File: SuggestPodcastController.java    From podcastpedia-web with MIT License 4 votes vote down vote up
@ExceptionHandler(HttpSessionRequiredException.class)
@ResponseStatus(value = HttpStatus.UNAUTHORIZED, reason = "The session has expired")
public String handleSessionExpired() {
	return "sessionExpired";
}