use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.
the class JspViewHandler method render.
public void render(String name, String page, String contentType, String encoding, String info, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
if (request == null) {
throw new ViewHandlerException("Null HttpServletRequest object");
}
if (UtilValidate.isEmpty(page)) {
throw new ViewHandlerException("Null or empty source");
}
// Debug.logInfo("Requested Page : " + page, module);
// Debug.logInfo("Physical Path : " + context.getRealPath(page));
// tell the ControlFilter we are forwarding
request.setAttribute(ControlFilter.FORWARDED_FROM_SERVLET, Boolean.TRUE);
RequestDispatcher rd = request.getRequestDispatcher(page);
if (rd == null) {
Debug.logInfo("HttpServletRequest.getRequestDispatcher() failed; trying ServletContext", module);
rd = context.getRequestDispatcher(page);
if (rd == null) {
Debug.logInfo("ServletContext.getRequestDispatcher() failed; trying ServletContext.getNamedDispatcher(\"jsp\")", module);
rd = context.getNamedDispatcher("jsp");
if (rd == null) {
throw new ViewHandlerException("Source returned a null dispatcher (" + page + ")");
}
}
}
try {
rd.include(request, response);
} catch (IOException ie) {
throw new ViewHandlerException("IO Error in view", ie);
} catch (ServletException e) {
Throwable throwable = e.getRootCause() != null ? e.getRootCause() : e;
if (throwable instanceof JspException) {
JspException jspe = (JspException) throwable;
throwable = jspe.getCause() != null ? jspe.getCause() : jspe;
}
Debug.logError(throwable, "ServletException rendering JSP view", module);
throw new ViewHandlerException(e.getMessage(), throwable);
}
}
use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.
the class ContentUrlFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
Delegator delegator = (Delegator) httpRequest.getSession().getServletContext().getAttribute("delegator");
String urlContentId = null;
String pathInfo = UtilHttp.getFullRequestUrl(httpRequest);
if (UtilValidate.isNotEmpty(pathInfo)) {
String alternativeUrl = pathInfo.substring(pathInfo.lastIndexOf('/'));
if (alternativeUrl.endsWith("-content")) {
try {
GenericValue contentDataResourceView = EntityQuery.use(delegator).from("ContentDataResourceView").where("drObjectInfo", alternativeUrl).orderBy("createdDate DESC").queryFirst();
if (contentDataResourceView != null) {
GenericValue content = EntityQuery.use(delegator).from("ContentAssoc").where("contentAssocTypeId", "ALTERNATIVE_URL", "contentIdTo", contentDataResourceView.get("contentId")).filterByDate().queryFirst();
if (content != null) {
urlContentId = content.getString("contentId");
}
}
} catch (GenericEntityException gee) {
Debug.logWarning(gee.getMessage(), module);
} catch (Exception e) {
Debug.logWarning(e.getMessage(), module);
}
}
if (UtilValidate.isNotEmpty(urlContentId)) {
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append("/" + WebAppUtil.CONTROL_MOUNT_POINT);
urlBuilder.append("/" + config.getInitParameter("viewRequest") + "?contentId=" + urlContentId);
// Set view query parameters
UrlServletHelper.setViewQueryParameters(request, urlBuilder);
Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module);
RequestDispatcher dispatch = request.getRequestDispatcher(urlBuilder.toString());
dispatch.forward(request, response);
return;
}
// Check path alias
UrlServletHelper.checkPathAlias(request, httpResponse, delegator, pathInfo);
}
// we're done checking; continue on
chain.doFilter(request, response);
}
use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.
the class SeoCatalogUrlServlet method doGet.
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Delegator delegator = (Delegator) getServletContext().getAttribute("delegator");
String pathInfo = request.getPathInfo();
List<String> pathElements = StringUtil.split(pathInfo, "/");
// look for productId
String productId = null;
try {
String lastPathElement = pathElements.get(pathElements.size() - 1);
if (lastPathElement.startsWith("p_") || EntityQuery.use(delegator).from("Product").where("productId", lastPathElement).cache().queryOne() != null) {
if (lastPathElement.startsWith("p_")) {
productId = lastPathElement.substring(2);
} else {
productId = lastPathElement;
}
pathElements.remove(pathElements.size() - 1);
}
} catch (GenericEntityException e) {
Debug.logError(e, "Error looking up product info for ProductUrl with path info [" + pathInfo + "]: " + e.toString(), module);
}
// get category info going with the IDs that remain
String categoryId = null;
if (pathElements.size() == 1) {
CategoryWorker.setTrail(request, pathElements.get(0), null);
categoryId = pathElements.get(0);
} else if (pathElements.size() == 2) {
CategoryWorker.setTrail(request, pathElements.get(1), pathElements.get(0));
categoryId = pathElements.get(1);
} else if (pathElements.size() > 2) {
List<String> trail = CategoryWorker.getTrail(request);
if (trail == null) {
trail = new LinkedList<String>();
}
if (trail.contains(pathElements.get(0))) {
// first category is in the trail, so remove it everything after that and fill it in with the list from the pathInfo
int firstElementIndex = trail.indexOf(pathElements.get(0));
while (trail.size() > firstElementIndex) {
trail.remove(firstElementIndex);
}
trail.addAll(pathElements);
} else {
// first category is NOT in the trail, so clear out the trail and use the pathElements list
trail.clear();
trail.addAll(pathElements);
}
CategoryWorker.setTrail(request, trail);
categoryId = pathElements.get(pathElements.size() - 1);
}
if (categoryId != null) {
request.setAttribute("productCategoryId", categoryId);
}
String rootCategoryId = null;
if (pathElements.size() >= 1) {
rootCategoryId = pathElements.get(0);
}
if (rootCategoryId != null) {
request.setAttribute("rootCategoryId", rootCategoryId);
}
if (productId != null) {
request.setAttribute("product_id", productId);
request.setAttribute("productId", productId);
}
RequestDispatcher rd = request.getRequestDispatcher("/" + (UtilValidate.isEmpty(SeoControlServlet.getControlServlet()) ? "" : (SeoControlServlet.getControlServlet() + "/")) + (productId != null ? PRODUCT_REQUEST : CATEGORY_REQUEST));
rd.forward(request, response);
}
use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.
the class CatalogUrlSeoTransform method forwardCategoryUri.
/**
* Forward a category uri according to forward pattern regular expressions.
* @param request
* @param response
* @param delegator
* @param controlServlet
* @return
* @throws ServletException
* @throws IOException
*/
public static boolean forwardCategoryUri(HttpServletRequest request, HttpServletResponse response, Delegator delegator, String controlServlet) throws ServletException, IOException {
String pathInfo = request.getRequestURI();
String contextPath = request.getContextPath();
if (!isCategoryMapInitialed()) {
initCategoryMap(request);
}
if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
return false;
}
List<String> pathElements = StringUtil.split(pathInfo, "/");
if (UtilValidate.isEmpty(pathElements)) {
return false;
}
String lastPathElement = pathElements.get(pathElements.size() - 1);
String categoryId = null;
if (UtilValidate.isNotEmpty(lastPathElement)) {
if (UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) {
if (lastPathElement.endsWith(SeoConfigUtil.getCategoryUrlSuffix())) {
lastPathElement = lastPathElement.substring(0, lastPathElement.length() - SeoConfigUtil.getCategoryUrlSuffix().length());
} else {
return false;
}
}
for (Entry<String, String> entry : categoryNameIdMap.entrySet()) {
String categoryName = entry.getKey();
if (lastPathElement.startsWith(categoryName)) {
categoryId = entry.getValue();
break;
}
}
if (UtilValidate.isEmpty(categoryId)) {
categoryId = lastPathElement.trim();
}
}
if (UtilValidate.isNotEmpty(categoryId)) {
request.setAttribute("productCategoryId", categoryId);
StringBuilder urlBuilder = new StringBuilder();
if (UtilValidate.isNotEmpty(controlServlet)) {
urlBuilder.append("/" + controlServlet);
}
urlBuilder.append("/" + CatalogUrlServlet.CATEGORY_REQUEST);
UrlServletHelper.setViewQueryParameters(request, urlBuilder);
Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module);
RequestDispatcher rd = request.getRequestDispatcher(urlBuilder.toString());
rd.forward(request, response);
return true;
}
return false;
}
use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.
the class CatalogUrlServlet method doGet.
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Delegator delegator = (Delegator) getServletContext().getAttribute("delegator");
String pathInfo = request.getPathInfo();
List<String> pathElements = StringUtil.split(pathInfo, "/");
String productId = null;
String categoryId = null;
if (pathElements == null) {
RequestDispatcher rd = request.getRequestDispatcher("/" + WebAppUtil.CONTROL_MOUNT_POINT + "/main");
rd.forward(request, response);
} else {
try {
String lastPathElement = pathElements.get(pathElements.size() - 1);
if (lastPathElement.startsWith("p_")) {
productId = lastPathElement.substring(2);
} else {
GenericValue productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", lastPathElement).cache(true).queryOne();
if (productCategory != null) {
categoryId = lastPathElement;
} else {
productId = lastPathElement;
}
}
pathElements.remove(pathElements.size() - 1);
} catch (GenericEntityException e) {
Debug.logError(e, "Error in looking up ProductUrl or CategoryUrl with path info [" + pathInfo + "]: " + e.toString(), module);
}
// get category info going with the IDs that remain
if (pathElements.size() == 1) {
CategoryWorker.setTrail(request, pathElements.get(0), null);
categoryId = pathElements.get(0);
} else if (pathElements.size() == 2) {
CategoryWorker.setTrail(request, pathElements.get(1), pathElements.get(0));
categoryId = pathElements.get(1);
} else if (pathElements.size() > 2) {
List<String> trail = CategoryWorker.getTrail(request);
if (trail == null) {
trail = new LinkedList<String>();
}
if (trail.contains(pathElements.get(0))) {
// first category is in the trail, so remove it everything after that and fill it in with the list from the pathInfo
int firstElementIndex = trail.indexOf(pathElements.get(0));
while (trail.size() > firstElementIndex) {
trail.remove(firstElementIndex);
}
trail.addAll(pathElements);
} else {
// first category is NOT in the trail, so clear out the trail and use the pathElements list
trail.clear();
trail.addAll(pathElements);
}
CategoryWorker.setTrail(request, trail);
categoryId = pathElements.get(pathElements.size() - 1);
}
if (categoryId != null) {
request.setAttribute("productCategoryId", categoryId);
}
String rootCategoryId = null;
if (pathElements.size() >= 1) {
rootCategoryId = pathElements.get(0);
}
if (rootCategoryId != null) {
request.setAttribute("rootCategoryId", rootCategoryId);
}
if (productId != null) {
request.setAttribute("product_id", productId);
request.setAttribute("productId", productId);
}
RequestDispatcher rd = request.getRequestDispatcher("/" + WebAppUtil.CONTROL_MOUNT_POINT + "/" + (productId != null ? PRODUCT_REQUEST : CATEGORY_REQUEST));
rd.forward(request, response);
}
}
Aggregations