-
Notifications
You must be signed in to change notification settings - Fork 682
/
DefaultAjaxRequestResolver.java
43 lines (37 loc) · 1.79 KB
/
DefaultAjaxRequestResolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package org.pac4j.core.http.ajax;
import org.pac4j.core.context.HttpConstants;
import org.pac4j.core.context.Pac4jConstants;
import org.pac4j.core.context.WebContext;
import org.pac4j.core.exception.HttpAction;
import org.pac4j.core.redirect.RedirectAction;
import org.pac4j.core.util.CommonHelper;
/**
* Default way to compute if a HTTP request is an AJAX one.
*
* @author Jerome Leleu
* @since 1.8.0
*/
public class DefaultAjaxRequestResolver implements AjaxRequestResolver, HttpConstants, Pac4jConstants {
@Override
public boolean isAjax(final WebContext context) {
final boolean xmlHttpRequest = AJAX_HEADER_VALUE.equalsIgnoreCase(context.getRequestHeader(AJAX_HEADER_NAME));
final boolean hasDynamicAjaxParameter = Boolean.TRUE.toString().equalsIgnoreCase(context.getRequestHeader(IS_AJAX_REQUEST));
final boolean hasDynamicAjaxHeader = Boolean.TRUE.toString().equalsIgnoreCase(context.getRequestParameter(IS_AJAX_REQUEST));
return xmlHttpRequest || hasDynamicAjaxParameter || hasDynamicAjaxHeader;
}
@Override
public RedirectAction buildAjaxResponse(final String url,final WebContext context) {
if ( CommonHelper.isBlank(context.getRequestParameter(FACES_PARTIAL_AJAX_PARAMETER))) {
if (CommonHelper.isNotBlank(url)) {
context.setResponseHeader(HttpConstants.LOCATION_HEADER, url);
}
throw HttpAction.unauthorized(context);
}
final StringBuilder buffer = new StringBuilder();
buffer.append("<?xml version='1.0' encoding='UTF-8'?>");
buffer.append("<partial-response>");
buffer.append("<redirect url=\"" + url.replaceAll("&", "&") + "\"></redirect>");
buffer.append("</partial-response>");
return RedirectAction.success(buffer.toString());
}
}