Search in sources :

Example 11 with WebRequest

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;
}
Also used : AbstractRequestWrapperFactory(org.apache.wicket.protocol.http.servlet.AbstractRequestWrapperFactory) WebRequest(org.apache.wicket.request.http.WebRequest) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 12 with 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(" ?>");
        }
    }
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) ServletWebResponse(org.apache.wicket.protocol.http.servlet.ServletWebResponse) WebRequest(org.apache.wicket.request.http.WebRequest) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 13 with WebRequest

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);
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(org.apache.wicket.request.http.WebResponse) WebRequest(org.apache.wicket.request.http.WebRequest)

Example 14 with WebRequest

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;
}
Also used : WebRequest(org.apache.wicket.request.http.WebRequest) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) WebRequest(org.apache.wicket.request.http.WebRequest) Request(org.apache.wicket.request.Request)

Example 15 with WebRequest

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;
}
Also used : InputStream(java.io.InputStream) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) IOException(java.io.IOException) URL(java.net.URL) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpURLConnection(java.net.HttpURLConnection) WebRequest(org.apache.wicket.request.http.WebRequest) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Aggregations

WebRequest (org.apache.wicket.request.http.WebRequest)15 WebResponse (org.apache.wicket.request.http.WebResponse)5 Request (org.apache.wicket.request.Request)4 ServletWebRequest (org.apache.wicket.protocol.http.servlet.ServletWebRequest)3 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)3 IOException (java.io.IOException)2 Cookie (javax.servlet.http.Cookie)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 Attributes (org.apache.wicket.request.resource.IResource.Attributes)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Page (org.apache.wicket.Page)1 ThreadContext (org.apache.wicket.ThreadContext)1