org.springframework.web.servlet.support.RequestDataValueProcessor Java Examples

The following examples show how to use org.springframework.web.servlet.support.RequestDataValueProcessor. 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: RedirectView.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Find the registered {@link RequestDataValueProcessor}, if any, and allow
 * it to update the redirect target URL.
 * @param targetUrl the given redirect URL
 * @return the updated URL or the same as URL as the one passed in
 */
protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
		HttpServletRequest request, HttpServletResponse response) {

	WebApplicationContext wac = getWebApplicationContext();
	if (wac == null) {
		wac = RequestContextUtils.findWebApplicationContext(request, getServletContext());
	}

	if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		RequestDataValueProcessor processor = wac.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
		return processor.processUrl(request, targetUrl);
	}

	return targetUrl;
}
 
Example #2
Source File: RedirectView.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Find the registered {@link RequestDataValueProcessor}, if any, and allow
 * it to update the redirect target URL.
 * @param targetUrl the given redirect URL
 * @return the updated URL or the same as URL as the one passed in
 */
protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
		HttpServletRequest request, HttpServletResponse response) {

	WebApplicationContext wac = getWebApplicationContext();
	if (wac == null) {
		wac = RequestContextUtils.findWebApplicationContext(request, getServletContext());
	}

	if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		RequestDataValueProcessor processor = wac.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
		return processor.processUrl(request, targetUrl);
	}

	return targetUrl;
}
 
Example #3
Source File: UrlTag.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public int doEndTag() throws JspException {
	String url = createUrl();

	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if ((processor != null) && (request instanceof HttpServletRequest)) {
		url = processor.processUrl((HttpServletRequest) request, url);
	}

	if (this.var == null) {
		// print the url to the writer
		try {
			pageContext.getOut().print(url);
		}
		catch (IOException ex) {
			throw new JspException(ex);
		}
	}
	else {
		// store the url as a variable
		pageContext.setAttribute(var, url, scope);
	}
	return EVAL_PAGE;
}
 
Example #4
Source File: UrlTag.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public int doEndTag() throws JspException {
	String url = createUrl();

	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if ((processor != null) && (request instanceof HttpServletRequest)) {
		url = processor.processUrl((HttpServletRequest) request, url);
	}

	if (this.var == null) {
		// print the url to the writer
		try {
			pageContext.getOut().print(url);
		}
		catch (IOException e) {
			throw new JspException(e);
		}
	}
	else {
		// store the url as a variable
		pageContext.setAttribute(var, url, scope);
	}
	return EVAL_PAGE;
}
 
Example #5
Source File: RedirectView.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the registered {@link RequestDataValueProcessor}, if any, and allow
 * it to update the redirect target URL.
 * @param targetUrl the given redirect URL
 * @return the updated URL or the same as URL as the one passed in
 */
protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
		HttpServletRequest request, HttpServletResponse response) {

	WebApplicationContext wac = getWebApplicationContext();
	if (wac == null) {
		wac = RequestContextUtils.findWebApplicationContext(request, getServletContext());
	}

	if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		RequestDataValueProcessor processor = wac.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
		return processor.processUrl(request, targetUrl);
	}

	return targetUrl;
}
 
Example #6
Source File: RedirectViewTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void updateTargetUrl() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
	wac.setServletContext(new MockServletContext());
	wac.refresh();

	RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
	wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);

	RedirectView rv = new RedirectView();
	rv.setApplicationContext(wac);	// Init RedirectView with WebAppCxt
	rv.setUrl("/path");

	MockHttpServletRequest request = createRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	HttpServletResponse response = new MockHttpServletResponse();

	given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");

	rv.render(new ModelMap(), request, response);

	verify(mockProcessor).processUrl(request, "/path");
}
 
Example #7
Source File: FormTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void requestDataValueProcessorHooks() throws Exception {
	String action = "/my/form?foo=bar";
	RequestDataValueProcessor processor = getMockRequestDataValueProcessor();
	given(processor.processAction(this.request, action, "post")).willReturn(action);
	given(processor.getExtraHiddenFields(this.request)).willReturn(Collections.singletonMap("key", "value"));

	this.tag.doStartTag();
	this.tag.doEndTag();
	this.tag.doFinally();

	String output = getOutput();

	assertEquals("<div>\n<input type=\"hidden\" name=\"key\" value=\"value\" />\n</div>", getInputTag(output));
	assertFormTagOpened(output);
	assertFormTagClosed(output);
}
 
Example #8
Source File: RedirectViewTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void updateTargetUrlWithContextLoader() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);

	MockServletContext servletContext = new MockServletContext();
	ContextLoader contextLoader = new ContextLoader(wac);
	contextLoader.initWebApplicationContext(servletContext);

	try {
		RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
		wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);

		RedirectView rv = new RedirectView();
		rv.setUrl("/path");

		given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
		rv.render(new ModelMap(), request, response);
		verify(mockProcessor).processUrl(request, "/path");
	}
	finally {
		contextLoader.closeWebApplicationContext(servletContext);
	}
}
 
Example #9
Source File: RedirectViewTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void updateTargetUrl() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
	wac.setServletContext(new MockServletContext());
	wac.refresh();

	RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
	wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);

	RedirectView rv = new RedirectView();
	rv.setApplicationContext(wac);	// Init RedirectView with WebAppCxt
	rv.setUrl("/path");

	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
	rv.render(new ModelMap(), request, response);
	verify(mockProcessor).processUrl(request, "/path");
}
 
Example #10
Source File: UrlTag.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public int doEndTag() throws JspException {
	String url = createUrl();

	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if ((processor != null) && (request instanceof HttpServletRequest)) {
		url = processor.processUrl((HttpServletRequest) request, url);
	}

	if (this.var == null) {
		// print the url to the writer
		try {
			this.pageContext.getOut().print(url);
		}
		catch (IOException ex) {
			throw new JspException(ex);
		}
	}
	else {
		// store the url as a variable
		this.pageContext.setAttribute(this.var, url, this.scope);
	}
	return EVAL_PAGE;
}
 
Example #11
Source File: FormTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void requestDataValueProcessorHooks() throws Exception {
	String action = "/my/form?foo=bar";
	RequestDataValueProcessor processor = getMockRequestDataValueProcessor();
	given(processor.processAction(this.request, action, "post")).willReturn(action);
	given(processor.getExtraHiddenFields(this.request)).willReturn(Collections.singletonMap("key", "value"));

	this.tag.doStartTag();
	this.tag.doEndTag();
	this.tag.doFinally();

	String output = getOutput();

	assertEquals("<div>\n<input type=\"hidden\" name=\"key\" value=\"value\" />\n</div>", getInputTag(output));
	assertFormTagOpened(output);
	assertFormTagClosed(output);
}
 
Example #12
Source File: RedirectView.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Find the registered {@link RequestDataValueProcessor}, if any, and allow
 * it to update the redirect target URL.
 * @param targetUrl the given redirect URL
 * @return the updated URL or the same as URL as the one passed in
 */
protected String updateTargetUrl(String targetUrl, Map<String, Object> model,
		HttpServletRequest request, HttpServletResponse response) {

	WebApplicationContext wac = getWebApplicationContext();
	if (wac == null) {
		wac = RequestContextUtils.findWebApplicationContext(request, getServletContext());
	}

	if (wac != null && wac.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		RequestDataValueProcessor processor = wac.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
		return processor.processUrl(request, targetUrl);
	}

	return targetUrl;
}
 
Example #13
Source File: FormTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void requestDataValueProcessorHooks() throws Exception {
	String action = "/my/form?foo=bar";
	RequestDataValueProcessor processor = getMockRequestDataValueProcessor();
	given(processor.processAction(this.request, action, "post")).willReturn(action);
	given(processor.getExtraHiddenFields(this.request)).willReturn(Collections.singletonMap("key", "value"));

	this.tag.doStartTag();
	this.tag.doEndTag();
	this.tag.doFinally();

	String output = getOutput();

	assertEquals("<div>\n<input type=\"hidden\" name=\"key\" value=\"value\" />\n</div>", getInputTag(output));
	assertFormTagOpened(output);
	assertFormTagClosed(output);
}
 
Example #14
Source File: RedirectViewTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void updateTargetUrlWithContextLoader() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);

	MockServletContext servletContext = new MockServletContext();
	ContextLoader contextLoader = new ContextLoader(wac);
	contextLoader.initWebApplicationContext(servletContext);

	try {
		RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
		wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);

		RedirectView rv = new RedirectView();
		rv.setUrl("/path");

		given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
		rv.render(new ModelMap(), request, response);
		verify(mockProcessor).processUrl(request, "/path");
	}
	finally {
		contextLoader.closeWebApplicationContext(servletContext);
	}
}
 
Example #15
Source File: RedirectViewTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void updateTargetUrl() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);
	wac.setServletContext(new MockServletContext());
	wac.refresh();

	RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
	wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);

	RedirectView rv = new RedirectView();
	rv.setApplicationContext(wac);	// Init RedirectView with WebAppCxt
	rv.setUrl("/path");

	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

	given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");
	rv.render(new ModelMap(), request, response);
	verify(mockProcessor).processUrl(request, "/path");
}
 
Example #16
Source File: UrlTag.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public int doEndTag() throws JspException {
	String url = createUrl();

	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if ((processor != null) && (request instanceof HttpServletRequest)) {
		url = processor.processUrl((HttpServletRequest) request, url);
	}

	if (this.var == null) {
		// print the url to the writer
		try {
			this.pageContext.getOut().print(url);
		}
		catch (IOException ex) {
			throw new JspException(ex);
		}
	}
	else {
		// store the url as a variable
		this.pageContext.setAttribute(this.var, url, this.scope);
	}
	return EVAL_PAGE;
}
 
Example #17
Source File: AbstractHtmlElementTagTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
protected RequestDataValueProcessor getMockRequestDataValueProcessor() {
	RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
	ServletRequest request = getPageContext().getRequest();
	StaticWebApplicationContext wac = (StaticWebApplicationContext) RequestContextUtils.getWebApplicationContext(request);
	wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
	return mockProcessor;
}
 
Example #18
Source File: RedirectViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void updateTargetUrlWithContextLoader() throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class);

	MockServletContext servletContext = new MockServletContext();
	ContextLoader contextLoader = new ContextLoader(wac);
	contextLoader.initWebApplicationContext(servletContext);

	try {
		RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
		wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);

		RedirectView rv = new RedirectView();
		rv.setUrl("/path");

		MockHttpServletRequest request = createRequest();
		HttpServletResponse response = new MockHttpServletResponse();

		given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123");

		rv.render(new ModelMap(), request, response);

		verify(mockProcessor).processUrl(request, "/path");
	}
	finally {
		contextLoader.closeWebApplicationContext(servletContext);
	}
}
 
Example #19
Source File: TagUtils.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
     * <p>
     * {@code Spring MVC} のパス修飾機構によって指定されたURLパスを修飾します。<br/>
     * </p>
     * @param action パス
     * @param requestContext {@link RequestContext} インスタンス
     * @param pageContext {@link PageContext} インスタンス
     * @return 修飾されたパス
     */
    public static String processAction(String action, RequestContext requestContext, PageContext pageContext) {
        RequestDataValueProcessor processor = requestContext.getRequestDataValueProcessor();
        ServletRequest request = pageContext.getRequest();
        if ((processor != null) && (request instanceof HttpServletRequest)) {
//            return processor.processAction((HttpServletRequest) request, action);
            return processor.processAction((HttpServletRequest) request, action, ((HttpServletRequest) request).getMethod());
        }
        return action;
    }
 
Example #20
Source File: JseRequestDataValueProcessor.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Map<String, String> getExtraHiddenFields(HttpServletRequest request) {
    Map<String, String> extraHiddenFields = new HashMap<String, String>();
    for (RequestDataValueProcessor requestDataValueProcessor : requestDataValueProcessors) {
        Map<String, String> m = requestDataValueProcessor.getExtraHiddenFields(request);
        if (m != null) {
            extraHiddenFields.putAll(m);
        }
    }
    return extraHiddenFields;
}
 
Example #21
Source File: AbstractDataBoundFormElementTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Process the given form field through a {@link RequestDataValueProcessor}
 * instance if one is configured or otherwise returns the same value.
 */
protected final String processFieldValue(String name, String value, String type) {
	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if (processor != null && (request instanceof HttpServletRequest)) {
		value = processor.processFormFieldValue((HttpServletRequest) request, name, value, type);
	}
	return value;
}
 
Example #22
Source File: FormTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the '{@code form}' block tag and removes the form object name
 * from the {@link javax.servlet.jsp.PageContext}.
 */
@Override
public int doEndTag() throws JspException {
	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if ((processor != null) && (request instanceof HttpServletRequest)) {
		writeHiddenFields(processor.getExtraHiddenFields((HttpServletRequest) request));
	}
	this.tagWriter.endTag();
	return EVAL_PAGE;
}
 
Example #23
Source File: JseRequestDataValueProcessor.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String processUrl(HttpServletRequest request, String url) {
    for (RequestDataValueProcessor requestDataValueProcessor : requestDataValueProcessors) {
        url = requestDataValueProcessor.processUrl(request, url);
    }
    return url;
}
 
Example #24
Source File: FormTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Process the action through a {@link RequestDataValueProcessor} instance
 * if one is configured or otherwise returns the action unmodified.
 */
private String processAction(String action) {
	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if (processor != null && request instanceof HttpServletRequest) {
		action = processor.processAction((HttpServletRequest) request, action, getHttpMethod());
	}
	return action;
}
 
Example #25
Source File: FormTag.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Process the action through a {@link RequestDataValueProcessor} instance
 * if one is configured or otherwise returns the action unmodified.
 */
private String processAction(String action) {
	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if (processor != null && request instanceof HttpServletRequest) {
		action = processor.processAction((HttpServletRequest) request, action, getHttpMethod());
	}
	return action;
}
 
Example #26
Source File: AbstractDataBoundFormElementTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Process the given form field through a {@link RequestDataValueProcessor}
 * instance if one is configured or otherwise returns the same value.
 */
protected final String processFieldValue(String name, String value, String type) {
	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if (processor != null && (request instanceof HttpServletRequest)) {
		value = processor.processFormFieldValue((HttpServletRequest) request, name, value, type);
	}
	return value;
}
 
Example #27
Source File: FormTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Closes the '{@code form}' block tag and removes the form object name
 * from the {@link javax.servlet.jsp.PageContext}.
 */
@Override
public int doEndTag() throws JspException {
	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if ((processor != null) && (request instanceof HttpServletRequest)) {
		writeHiddenFields(processor.getExtraHiddenFields((HttpServletRequest) request));
	}
	this.tagWriter.endTag();
	return EVAL_PAGE;
}
 
Example #28
Source File: FormTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Process the action through a {@link RequestDataValueProcessor} instance
 * if one is configured or otherwise returns the action unmodified.
 */
private String processAction(String action) {
	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if (processor != null && request instanceof HttpServletRequest) {
		action = processor.processAction((HttpServletRequest) request, action, getHttpMethod());
	}
	return action;
}
 
Example #29
Source File: AbstractHtmlElementTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected RequestDataValueProcessor getMockRequestDataValueProcessor() {
	RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class);
	HttpServletRequest request = (HttpServletRequest) getPageContext().getRequest();
	WebApplicationContext wac = RequestContextUtils.findWebApplicationContext(request);
	wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor);
	return mockProcessor;
}
 
Example #30
Source File: AbstractDataBoundFormElementTag.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Process the given form field through a {@link RequestDataValueProcessor}
 * instance if one is configured or otherwise returns the same value.
 */
protected final String processFieldValue(@Nullable String name, String value, String type) {
	RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
	ServletRequest request = this.pageContext.getRequest();
	if (processor != null && request instanceof HttpServletRequest) {
		value = processor.processFormFieldValue((HttpServletRequest) request, name, value, type);
	}
	return value;
}