use of org.apache.wicket.request.http.WebRequest in project wicket by apache.
the class WebApplication method createWebRequest.
/**
* Pre- and post- configures the {@link WebRequest} created by user override-able
* {@link #newWebRequest(HttpServletRequest, String)}
*
* @param servletRequest
* the current HTTP Servlet request
* @param filterPath
* the filter mapping read from web.xml
* @return a WebRequest object
*/
WebRequest createWebRequest(HttpServletRequest servletRequest, final String filterPath) {
if (hasFilterFactoryManager()) {
for (AbstractRequestWrapperFactory factory : getFilterFactoryManager()) {
servletRequest = factory.getWrapper(servletRequest);
}
}
WebRequest webRequest = newWebRequest(servletRequest, filterPath);
if (servletRequest.getCharacterEncoding() == null) {
try {
if (webRequest.isAjax()) {
// WICKET-3908, WICKET-1816: Forms submitted with Ajax are always UTF-8 encoded
servletRequest.setCharacterEncoding(CharEncoding.UTF_8);
} else {
String requestEncoding = getRequestCycleSettings().getResponseRequestEncoding();
servletRequest.setCharacterEncoding(requestEncoding);
}
} catch (UnsupportedEncodingException e) {
throw new WicketRuntimeException(e);
}
}
return webRequest;
}
use of org.apache.wicket.request.http.WebRequest in project wicket by apache.
the class WebApplication method renderXmlDecl.
/**
* The rules if and when to insert an xml decl in the response are a bit tricky. Hence, we allow
* the user to replace the default implementation per page and per application.
* <p>
* Default implementation: the page mime type must be "application/xhtml+xml" and request
* HTTP_ACCEPT header must include "application/xhtml+xml" to automatically include the xml
* decl. Please see <a href=
* "https://developer.mozilla.org/en/Writing_JavaScript_for_XHTML#Finally:_Content_Negotiation"
* >Writing JavaScript for XHTML</a> for details.
* <p>
* Please note that xml decls in Wicket's markup are only used for reading the markup. The
* markup's xml decl will always be removed and never be used to configure the response.
*
* @param page
* The page currently being rendered
* @param insert
* If false, than the rules are applied. If true, it'll always be written. In order
* to never insert it, than subclass renderXmlDecl() with an empty implementation.
*/
public void renderXmlDecl(final WebPage page, boolean insert) {
if (insert || MarkupType.XML_MIME.equalsIgnoreCase(page.getMarkupType().getMimeType())) {
final RequestCycle cycle = RequestCycle.get();
if (insert == false) {
WebRequest request = (WebRequest) cycle.getRequest();
String accept = request.getHeader("Accept");
insert = ((accept == null) || (accept.indexOf(MarkupType.XML_MIME) != -1));
}
if (insert) {
WebResponse response = (WebResponse) cycle.getResponse();
response.write("<?xml version='1.0'");
String encoding = getRequestCycleSettings().getResponseRequestEncoding();
if (Strings.isEmpty(encoding) == false) {
response.write(" encoding='");
response.write(encoding);
response.write("'");
}
response.write(" ?>");
}
}
}
use of org.apache.wicket.request.http.WebRequest in project wicket by apache.
the class CookiePage method onConfigure.
@Override
protected void onConfigure() {
super.onConfigure();
Cookie cookie = ((WebRequest) getRequest()).getCookie(cookieName);
Assert.assertEquals(cookieValue, cookie.getValue());
WebResponse response = (WebResponse) getResponse();
response.addCookie(cookie);
}
use of org.apache.wicket.request.http.WebRequest in project wicket by apache.
the class DefaultExceptionMapper method isProcessingAjaxRequest.
/**
* @return true if current request is an AJAX request, false otherwise.
*/
protected boolean isProcessingAjaxRequest() {
RequestCycle rc = RequestCycle.get();
Request request = rc.getRequest();
if (request instanceof WebRequest) {
return ((WebRequest) request).isAjax();
}
return false;
}
use of org.apache.wicket.request.http.WebRequest in project wicket-orientdb by OrienteerBAP.
the class OrientDBHttpAPIResource method newResourceResponse.
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
final WebRequest request = (WebRequest) attributes.getRequest();
final HttpServletRequest httpRequest = (HttpServletRequest) request.getContainerRequest();
final PageParameters params = attributes.getParameters();
final ResourceResponse response = new ResourceResponse();
if (response.dataNeedsToBeWritten(attributes)) {
String orientDbHttpURL = OrientDbWebApplication.get().getOrientDbSettings().getOrientDBRestApiUrl();
if (orientDbHttpURL != null) {
StringBuilder sb = new StringBuilder(orientDbHttpURL);
for (int i = 0; i < params.getIndexedCount(); i++) {
// replace provided database name
String segment = i == 1 ? OrientDbWebSession.get().getDatabase().getName() : params.get(i).toString();
sb.append(UrlEncoder.PATH_INSTANCE.encode(segment, "UTF8")).append('/');
}
if (sb.charAt(sb.length() - 1) == '/')
sb.setLength(sb.length() - 1);
String queryString = request.getUrl().getQueryString();
if (!Strings.isEmpty(queryString)) {
if (sb.charAt(sb.length() - 1) != '?')
sb.append('?');
sb.append(queryString);
}
final String url = sb.toString();
final StringWriter sw = new StringWriter();
final PrintWriter out = new PrintWriter(sw);
HttpURLConnection con = null;
try {
URL orientURL = new URL(url);
con = (HttpURLConnection) orientURL.openConnection();
con.setDoInput(true);
con.setUseCaches(false);
String method = httpRequest.getMethod();
con.setRequestMethod(method);
con.setUseCaches(false);
if ("post".equalsIgnoreCase(method) || "put".equalsIgnoreCase(method)) {
int contentLength = httpRequest.getContentLength();
con.setDoOutput(true);
String initialContentType = httpRequest.getContentType();
if (initialContentType != null)
con.setRequestProperty("Content-Type", initialContentType);
int copied = IOUtils.copy(httpRequest.getInputStream(), con.getOutputStream());
if (contentLength > 0 && copied == 0) {
IOUtils.copy(httpRequest.getInputStream(), con.getOutputStream());
}
}
IOUtils.copy(con.getInputStream(), out, "UTF-8");
response.setStatusCode(con.getResponseCode());
response.setContentType(con.getContentType());
} catch (IOException e) {
LOG.error("Can't communicate with OrientDB REST", e);
if (con != null) {
try {
response.setError(con.getResponseCode(), con.getResponseMessage());
InputStream errorStream = con.getErrorStream();
if (errorStream != null) {
IOUtils.copy(errorStream, out, "UTF-8");
String errorBody = sw.toString();
LOG.error(errorBody);
attributes.getResponse().write(errorBody);
}
} catch (IOException e1) {
LOG.error("Can't response by error", e1);
}
}
} finally {
if (con != null)
con.disconnect();
}
response.setWriteCallback(new WriteCallback() {
@Override
public void writeData(Attributes attributes) throws IOException {
attributes.getResponse().write(sw.toString());
}
});
} else {
response.setError(HttpServletResponse.SC_BAD_GATEWAY, "OrientDB REST URL is not specified");
}
}
return response;
}
Aggregations