/** * Send a permanent redirect (HTTP status 301 "Moved Permanently") response with the given target * path. * * <p>The given target path may be relative or absolute. If it is relative, it will be resolved * against the request URL. * * @param targetPath the path to which the client is redirected. * @param req the HttpServletRequest * @param res the HttpServletResponse * @throws IOException if can't write the response. */ public static void sendPermanentRedirect( String targetPath, HttpServletRequest req, HttpServletResponse res) throws IOException { // Absolute URL needed so resolve the target path against the request URL. URI uri; try { uri = new URI(req.getRequestURL().toString()); } catch (URISyntaxException e) { log.error( "sendPermanentRedirect(): Bad syntax on request URL <" + req.getRequestURL() + ">.", e); log.info( "sendPermanentRedirect(): " + UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 0)); if (!res.isCommitted()) res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } String absolutePath = uri.resolve(targetPath).toString(); absolutePath = res.encodeRedirectURL(absolutePath); res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); res.addHeader("Location", absolutePath); String title = "Permanently Moved - 301"; String body = new StringBuilder() .append("<p>") .append("The requested URL <") .append(req.getRequestURL()) .append("> has been permanently moved (HTTP status code 301).") .append(" Instead, please use the following URL: <a href=\"") .append(absolutePath) .append("\">") .append(absolutePath) .append("</a>.") .append("</p>") .toString(); String htmlResp = new StringBuilder() .append(HtmlWriter.getInstance().getHtmlDoctypeAndOpenTag()) .append("<head><title>") .append(title) .append("</title></head><body>") .append("<h1>") .append(title) .append("</h1>") .append(body) .append("</body></html>") .toString(); log.info("sendPermanentRedirect(): redirect to " + absolutePath); log.info( "sendPermanentRedirect(): " + UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_MOVED_PERMANENTLY, htmlResp.length())); // Write the catalog out. PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.print(htmlResp); out.flush(); }
/** * Allows one to forward the following request and response using the request dispatcher. * * @param resource the resource to forward the request/response to. * @param request the http request to forward. * @param response the http response to forward. * @throws javax.servlet.ServletException * @throws java.io.IOException */ protected void redirect(String resource, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect(response.encodeRedirectURL(resource)); }
// The tck uses only get & post requests protected void processTCKReq(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LOGGER.entering(LOG_CLASS, "servlet entry"); PortletRequest portletReq = (PortletRequest) request.getAttribute("javax.portlet.request"); PortletResponse portletResp = (PortletResponse) request.getAttribute("javax.portlet.response"); PortletConfig portletConfig = (PortletConfig) request.getAttribute("javax.portlet.config"); long svtTid = Thread.currentThread().getId(); long reqTid = (Long) portletReq.getAttribute(THREADID_ATTR); PrintWriter writer = ((MimeResponse) portletResp).getWriter(); JSR286DispatcherReqRespTestCaseDetails tcd = new JSR286DispatcherReqRespTestCaseDetails(); // Create result objects for the tests /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_containsHeader */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.containsHeader must return false" */ TestResult tr0 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_CONTAINSHEADER); try { boolean ok = response.containsHeader("Accept"); tr0.setTcSuccess(ok == false); } catch (Exception e) { tr0.appendTcDetail(e.toString()); } tr0.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_encodeRedirectURL1 */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.encodeRedirectURL must return null" */ TestResult tr1 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_ENCODEREDIRECTURL1); try { String isval = response.encodeRedirectURL("http://www.cnn.com/"); CompareUtils.stringsEqual(isval, null, tr1); } catch (Exception e) { tr1.appendTcDetail(e.toString()); } tr1.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_encodeRedirectUrl */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.encodeRedirectUrl must return null" */ TestResult tr2 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_ENCODEREDIRECTURL); try { String isval = response.encodeRedirectUrl("http://www.cnn.com/"); CompareUtils.stringsEqual(isval, null, tr2); } catch (Exception e) { tr2.appendTcDetail(e.toString()); } tr2.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_encodeURL1 */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.encodeURL must provide the same */ /* functionality as ResourceResponse.encodeURL" */ TestResult tr3 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_ENCODEURL1); try { String turl = "http://www.apache.org/"; String hval = (String) response.encodeURL(turl); String pval = (String) portletResp.encodeURL(turl); CompareUtils.stringsEqual("HttpServletResponse", hval, "ResourceResponse", pval, tr3); } catch (Exception e) { tr3.appendTcDetail(e.toString()); } tr3.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_encodeUrl */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.encodeUrl must provide the same */ /* functionality as ResourceResponse.encodeURL" */ TestResult tr4 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_ENCODEURL); try { String turl = "http://www.apache.org/"; String hval = (String) response.encodeUrl(turl); String pval = (String) portletResp.encodeURL(turl); CompareUtils.stringsEqual("HttpServletResponse", hval, "ResourceResponse", pval, tr4); } catch (Exception e) { tr4.appendTcDetail(e.toString()); } tr4.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_getBufferSize */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.getBufferSize must provide the same */ /* functionality as ResourceResponse.getBufferSize" */ TestResult tr5 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_GETBUFFERSIZE); try { int hval = response.getBufferSize(); int pval = ((ResourceResponse) portletResp).getBufferSize(); String str = "Value " + hval + " from " + "HttpServletResponse" + " does not equal value " + pval + " + ResourceResponse"; if (hval != pval) { tr5.appendTcDetail(str); } tr5.setTcSuccess(hval == pval); } catch (Exception e) { tr5.appendTcDetail(e.toString()); } tr5.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_getCharacterEncoding */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.getCharacterEncoding must provide */ /* the same functionality as ResourceResponse.getCharacterEncoding" */ TestResult tr6 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_GETCHARACTERENCODING); try { String hval = response.getCharacterEncoding(); String pval = ((ResourceResponse) portletResp).getCharacterEncoding(); CompareUtils.stringsEqual("HttpServletResponse", hval, "ResourceResponse", pval, tr6); } catch (Exception e) { tr6.appendTcDetail(e.toString()); } tr6.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_getContentType */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.getContentType must provide the */ /* same functionality as ResourceResponse.getContentType" */ TestResult tr7 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_GETCONTENTTYPE); try { String hval = response.getContentType(); String pval = ((ResourceResponse) portletResp).getContentType(); CompareUtils.stringsEqual("HttpServletResponse", hval, "ResourceResponse", pval, tr7); } catch (Exception e) { tr7.appendTcDetail(e.toString()); } tr7.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_getLocale */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.getLocale must provide the same */ /* functionality as ResourceResponse.getLocale" */ TestResult tr8 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_GETLOCALE); try { Locale hl = response.getLocale(); Locale pl = ((MimeResponse) portletResp).getLocale(); String hval = hl.getDisplayName(); String pval = pl.getDisplayName(); CompareUtils.stringsEqual("HttpServletResponse", hval, "ResourceResponse", pval, tr8); } catch (Exception e) { tr8.appendTcDetail(e.toString()); } tr8.writeTo(writer); /* TestCase: V2DispatcherReqRespTests4_SPEC2_19_IncludeServletResourceResponse_isCommitted */ /* Details: "In a target servlet of a include in the Resource phase, */ /* the method HttpServletResponse.isCommitted must provide the same */ /* functionality as ResourceResponse.isCommitted" */ TestResult tr9 = tcd.getTestResultFailed( V2DISPATCHERREQRESPTESTS4_SPEC2_19_INCLUDESERVLETRESOURCERESPONSE_ISCOMMITTED); try { boolean hval = response.isCommitted(); boolean pval = ((ResourceResponse) portletResp).isCommitted(); String str = "Value " + hval + " from " + "HttpServletResponse" + " does not equal value " + pval + " + ResourceResponse"; if (hval != pval) { tr9.appendTcDetail(str); } tr9.setTcSuccess(hval == pval); } catch (Exception e) { tr9.appendTcDetail(e.toString()); } tr9.writeTo(writer); }