From b343d7b6de52660a25826fcf8683581f062bb0dc Mon Sep 17 00:00:00 2001 From: gkessler Date: Wed, 30 Jun 2010 19:50:24 +0000 Subject: [316926] JSFFileURL#getFileURL fails when JSF version does not match its default JEE version --- .../jsf/core/internal/project/facet/J2EEUtils.java | 461 +++++++++++++++++++ .../jsf/core/internal/project/facet/JEEUtils.java | 344 ++++++++++++++ .../project/facet/JSFFacetInstallDelegate.java | 18 +- .../jsf/core/internal/project/facet/JSFUtils.java | 273 ++++++++++- .../core/internal/project/facet/JSFUtils11.java | 507 +-------------------- .../core/internal/project/facet/JSFUtils12.java | 391 +--------------- 6 files changed, 1095 insertions(+), 899 deletions(-) create mode 100644 jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/J2EEUtils.java create mode 100644 jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JEEUtils.java diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/J2EEUtils.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/J2EEUtils.java new file mode 100644 index 000000000..80087aec5 --- /dev/null +++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/J2EEUtils.java @@ -0,0 +1,461 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation and/or initial documentation + *******************************************************************************/ + +package org.eclipse.jst.jsf.core.internal.project.facet; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jst.j2ee.common.CommonFactory; +import org.eclipse.jst.j2ee.common.ParamValue; +import org.eclipse.jst.j2ee.internal.J2EEVersionConstants; +import org.eclipse.jst.j2ee.webapplication.ContextParam; +import org.eclipse.jst.j2ee.webapplication.Servlet; +import org.eclipse.jst.j2ee.webapplication.ServletMapping; +import org.eclipse.jst.j2ee.webapplication.ServletType; +import org.eclipse.jst.j2ee.webapplication.WebApp; +import org.eclipse.jst.j2ee.webapplication.WebapplicationFactory; + +/** + * Web.xml access for J2EE applications (web version 2.3 and 2.4) + */ +public class J2EEUtils { + + /** + * Finds and returns a Servlet definition, or null if servlet is not defined. + * + * @param webApp + * @param servletName + * @return Servlet or null + */ + public static Servlet findServlet(final WebApp webApp, String servletName) + { + if (webApp == null) + { + return null; + } + + final Iterator it = webApp.getServlets().iterator(); + + while (it.hasNext()) + { + final Servlet servlet = (Servlet) it.next(); + if (servlet != null && servlet.getWebType() != null) + { + if (servlet.getWebType().isServletType()) + { + if (((ServletType) servlet.getWebType()).getClassName() != null + && ((ServletType) servlet.getWebType()) + .getClassName().trim().equals(servletName)) + { + return servlet; + } + } + } + } + + // if we get to here then we have finished the loop + // without finding the servlet we're looking for + return null; + } + + /** + * Creates servlet reference in WebApp if not present or updates servlet + * name if found using the passed configuration. + * + * @param webApp + * @param displayName + * @param className + * @param servlet + * @return Servlet servlet - if passed servlet was null, will return created + * servlet + */ + public static Servlet createOrUpdateServletRef(final WebApp webApp, + String displayName, String className, Servlet servlet) + { + if (servlet == null) + { + // Create the servlet instance and set up the parameters from data + // model + servlet = WebapplicationFactory.eINSTANCE.createServlet(); + servlet.setServletName(displayName); + + ServletType servletType = WebapplicationFactory.eINSTANCE + .createServletType(); + servletType.setClassName(className); + servlet.setWebType(servletType); + servlet.setLoadOnStartup(Integer.valueOf(1)); + // Add the servlet to the web application model + webApp.getServlets().add(servlet); + } + else + { + // update + updateServletMappings(webApp, servlet, displayName); + servlet.setServletName(displayName); + servlet.setLoadOnStartup(Integer.valueOf(1)); + } + return servlet; + } + + /** + * Updates servlet mapping + * + * @param webApp + * @param servlet + * @param displayName + */ + public static void updateServletMappings(final WebApp webApp, + final Servlet servlet, final String displayName) + { + List mappings = findServletMappings(webApp, servlet); + for (ServletMapping map : mappings) + { + map.setName(displayName); + } + + } + + /** + * Finds mapping for given servlet + * + * @param webApp + * @param servlet + * @return List of mappings + */ + public static List findServletMappings(final WebApp webApp, final Servlet servlet) { + String servletName = servlet.getServletName(); + servletName = servletName != null ? servletName.trim() : servletName; + final List mappings = new ArrayList(); + final List allMappings = webApp.getServletMappings(); + for (int i=allMappings.size()-1;i>=0;--i){ + ServletMapping mapping = allMappings.get(i); + if (mapping != null && + mapping.getServlet() != null && + mapping.getServlet().getServletName() != null && + mapping.getServlet().getServletName().trim().equals(servletName)) + mappings.add(mapping); + } + return mappings; + } + + /** + * Creates servlet-mappings for the servlet + * + * @param webApp + * @param urlMappingList - list of string values to be used in url-pattern for servlet-mapping + * @param servlet + */ + public static void setUpURLMappings(final WebApp webApp, final List urlMappingList, + final Servlet servlet) { + // Add mappings + Iterator it = urlMappingList.iterator(); + while (it.hasNext()) { + String pattern = (String) it.next(); + if (!(doesServletMappingExist(webApp, servlet, pattern))){ + ServletMapping mapping = WebapplicationFactory.eINSTANCE + .createServletMapping(); + mapping.setServlet(servlet); + mapping.setName(servlet.getServletName()); + mapping.setUrlPattern(pattern); + webApp.getServletMappings().add(mapping); + } + } + } + + /** + * Checks whether given mapping exists for the given servlet + * @param webApp + * @param servlet + * @param pattern + * @return true or false + */ + public static boolean doesServletMappingExist(final WebApp webApp, final Servlet servlet, + final String pattern) { + + List mappings = webApp.getServletMappings(); + String servletName = servlet.getServletName(); + if (servletName != null) + { + final Iterator it = mappings.iterator(); + while(it.hasNext()) + { + ServletMapping mapping = (ServletMapping) it.next(); + if (mapping != null && + mapping.getServlet() != null && + mapping.getServlet().getServletName() != null && + mapping.getServlet().getServletName().trim().equals(servletName) && + mapping.getUrlPattern() != null && + mapping.getUrlPattern().trim().equals(pattern)) { + return true; + } + } + } + return false; + } + + /** + * Removes servlet-mappings for servlet using servlet-name. + * @param webApp + * @param servlet + */ + public static void removeURLMappings(final WebApp webApp, final Servlet servlet) { + List mappings = webApp.getServletMappings(); + String servletName = servlet.getServletName(); + if (servletName != null) { + for (int i=mappings.size()-1;i>=0;--i){ + ServletMapping mapping = (ServletMapping)mappings.get(i); + if (mapping != null && + mapping.getServlet() != null && + mapping.getServlet().getServletName() != null && + mapping.getServlet().getServletName().trim() + .equals(servletName)) { + mappings.remove(mapping); + } + } + } + } + + /** + * Removes servlet definition + * @param webApp + * @param servlet + */ + public static void removeServlet(final WebApp webApp, final Servlet servlet) { + webApp.getServlets().remove(servlet); + } + + /** + * Removes context-param + * @param webApp + * @param paramName + */ + public static void removeContextParam(final WebApp webApp, String paramName) { + if ("2.3".equals(webApp.getVersion())) //$NON-NLS-1$ + { + Iterator it = webApp.getContexts().iterator(); + while (it.hasNext()) + { + final ContextParam cp = (ContextParam) it.next(); + if (cp.getParamName().equals(paramName)) + { + webApp.getContexts().remove(cp); + break; + } + } + } + else if ("2.4".equals(webApp.getVersion())) //$NON-NLS-1$ + { + Iterator it = webApp.getContextParams().iterator(); + while (it.hasNext()) + { + ParamValue cp = (ParamValue) it.next(); + if (cp.getName().equals(paramName)) { + webApp.getContextParams().remove(cp); + break; + } + } + } + } + + + /** + * Creates or updates context-param in v 2.3 WebApp + * @param webApp + * @param paramName + * @param paramValue + */ + public static void setupContextParamForV2_3(final WebApp webApp, String paramName, String paramValue) { + + // if not default name and location, then add context param + ContextParam cp = null; + ContextParam foundCP = null; + boolean found = false; + // check to see if present + Iterator it = webApp.getContexts().iterator(); + while (it.hasNext()) { + cp = (ContextParam) it.next(); + if (cp != null && + cp.getParamName() != null && + cp.getParamName().equals(paramName)) { + foundCP = cp; + found = true; + } + } + if (!found) { + cp = WebapplicationFactory.eINSTANCE.createContextParam(); + cp.setParamName(paramName); + cp.setParamValue(paramValue); + webApp.getContexts().add(cp); + } else { + cp = foundCP; + if (cp.getParamValue().indexOf(paramValue) < 0) { + String curVal = cp.getParamValue(); + String val = paramValue; + if (curVal != null && !"".equals(curVal.trim())) { //$NON-NLS-1$ + val = curVal + ",\n" + val; //$NON-NLS-1$ + } + cp.setParamValue(val); + } + } + } + + + /** + * Creates or updates context-param in v 2.4 WebApp + * @param webApp + * @param paramName + * @param paramValue + */ + public static void setupContextParamForV2_4(final WebApp webApp, String paramName, String paramValue) { + // if not default name and location, then add context param + ParamValue foundCP = null; + ParamValue cp = null; + boolean found = false; + // check to see if present + Iterator it = webApp.getContextParams().iterator(); + while (it.hasNext()) { + cp = (ParamValue) it.next(); + if (cp != null && + cp.getName() != null && + cp.getName().trim().equals(paramName)) { + foundCP = cp; + found = true; + } + } + if (!found) { + ParamValue pv = CommonFactory.eINSTANCE.createParamValue(); + pv.setName(paramName); + pv.setValue(paramValue); + webApp.getContextParams().add(pv); + } else { + cp = foundCP; + if (cp.getValue().indexOf(paramValue) < 0) { + String curVal = cp.getValue(); + String val = paramValue; + if (curVal != null && !"".equals(curVal.trim())) { //$NON-NLS-1$ + val = curVal + ",\n" + val; //$NON-NLS-1$ + } + cp.setValue(val); + } + } + } + + /** + * Creates or updates context-param + * @param webApp + * @param paramName + * @param paramValue + */ + public static void setupContextParam(final WebApp webApp, String paramName, String paramValue) { + if (webApp.getVersionID() == J2EEVersionConstants.WEB_2_3_ID)//shouldn't have to do it this way, but that's the way it goes 119442 + { + setupContextParamForV2_3(webApp, paramName, paramValue); + } + else if (webApp.getVersionID() == J2EEVersionConstants.WEB_2_4_ID) + { + setupContextParamForV2_4(webApp, paramName, paramValue); + } + else + { + throw new IllegalArgumentException("Invalid argument: "+webApp.getVersionID()); //$NON-NLS-1$ + } + } + + /** + * @param webApp + * @param paramName + * @return context-param value or null if context-param is not found + */ + public static String getContextParam(final WebApp webApp, String paramName) + { + if ("2.3".equals(webApp.getVersion())) //$NON-NLS-1$ + { + for (Iterator it = webApp.getContexts().iterator(); it.hasNext();) + { + ContextParam cp = (ContextParam) it.next(); + if (cp != null && + cp.getParamName() != null && + cp.getParamName().trim().equals(paramName)) { + return cp.getParamValue(); + } + } + } + else if ("2.4".equals(webApp.getVersion())) //$NON-NLS-1$ + { + for (Iterator it = webApp.getContextParams().iterator(); it.hasNext();) + { + ParamValue cp = (ParamValue) it.next(); + if (cp != null && + cp.getName() != null && + cp.getName().trim().equals(paramName)) { + return cp.getValue(); + } + } + } + return null; + } + + /** + * @param map + * @return extension from map. Will return null if file extension not found + * in url patterns. + */ + public static String getFileExtensionFromMap(final ServletMapping map) + { + final String urlPattern = map.getUrlPattern(); + if (urlPattern != null + && urlPattern.trim().length() != 0) + { + IPath extPath = new Path(map.getUrlPattern()); + if (extPath != null) + { + String ext = extPath.getFileExtension(); + if (ext != null && ext.trim().length() != 0) + { + return ext; + } + } + } + return null; + } + + /** + * @param map + * @return prefix mapping. may return null. + */ + public static String getPrefixMapping(final ServletMapping map) + { + final String urlPattern = map.getUrlPattern(); + if (urlPattern != null && urlPattern.trim().length() != 0) + { + IPath extPath = new Path(urlPattern); + if (extPath != null) + { + String ext = extPath.getFileExtension(); + if (ext == null) + { + String lastSeg = extPath.lastSegment(); + if (lastSeg != null && lastSeg.equals("*")) //$NON-NLS-1$ + { + return extPath.removeLastSegments(1).toString(); + } + + return extPath.toString(); + } + } + } + return null; + } +} diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JEEUtils.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JEEUtils.java new file mode 100644 index 000000000..d3d20f644 --- /dev/null +++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JEEUtils.java @@ -0,0 +1,344 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation and/or initial documentation + *******************************************************************************/ + +package org.eclipse.jst.jsf.core.internal.project.facet; + +import java.util.Iterator; +import java.util.List; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jst.javaee.core.JavaeeFactory; +import org.eclipse.jst.javaee.core.ParamValue; +import org.eclipse.jst.javaee.core.UrlPatternType; +import org.eclipse.jst.javaee.web.Servlet; +import org.eclipse.jst.javaee.web.ServletMapping; +import org.eclipse.jst.javaee.web.WebApp; +import org.eclipse.jst.javaee.web.WebFactory; + +/** + * Web.xml access for JEE applications (web version 2.5 and above) + */ +public class JEEUtils { + /** + * Finds and returns a Servlet definition, or null if servlet is not defined. + * + * @param webApp + * @param servletName + * @return Servlet or null + */ + public static Servlet findServlet(final WebApp webApp, String servletName) + { + if (webApp == null) + { + return null; + } + + for (final Servlet servlet : webApp.getServlets()) + { + if (servlet != null && + servlet.getServletClass() != null + && servlet.getServletClass().trim().equals(servletName)) + { + return servlet; + } + } + + // if we get to here then we have finished the loop + // without finding the servlet we're looking for + return null; + } + + /** + * Creates servlet reference in WebApp if not present or updates servlet name if found + * using the passed configuration. + * + * @param webApp + * @param displayName + * @param className + * @param servlet + * @return Servlet servlet - if passed servlet was null, will return created servlet + */ + public static Servlet createOrUpdateServletRef(final WebApp webApp, + String displayName, String className, Servlet servlet) { + + if (servlet == null){ + // Create the servlet instance and set up the parameters from data + // model + servlet = WebFactory.eINSTANCE.createServlet(); + servlet.setServletName(displayName); + servlet.setServletClass(className); + servlet.setLoadOnStartup(Integer.valueOf(1)); + // Add the servlet to the web application model + webApp.getServlets().add(servlet); + + } else { + updateServletMappings(webApp, servlet, displayName); + servlet.setServletName(displayName); + servlet.setLoadOnStartup(Integer.valueOf(1)); + } + return servlet; + } + + /** + * Updates servlet mapping + * + * @param webApp + * @param servlet + * @param displayName + */ + public static void updateServletMappings(final WebApp webApp, + final Servlet servlet, final String displayName) + { + // update mappings for new name + ServletMapping mapping = findServletMapping(webApp, servlet); + if (mapping != null) + { + mapping.setServletName(displayName); + } + } + + /** + * Finds mapping for given servlet + * + * @param webApp + * @param servlet + * @return List of mappings + */ + public static ServletMapping findServletMapping(final WebApp webApp, final Servlet servlet) { + for (Iterator it=webApp.getServletMappings().iterator();it.hasNext();){ + ServletMapping mapping = (ServletMapping)it.next(); + if (mapping.getServletName() != null && + servlet.getServletName() != null && + mapping.getServletName().trim().equals(servlet.getServletName().trim())) + return mapping; + } + return null; + } + + /** + * Creates servlet-mappings for the servlet for 2.5 WebModules or greated + * + * @param webApp + * @param urlMappingList + * - list of string values to be used in url-pattern for + * servlet-mapping + * @param servlet + */ + public static void setUpURLMappings(final WebApp webApp, + final List urlMappingList, final Servlet servlet) + { + + if (urlMappingList.size() > 0) + { + ServletMapping mapping = findServletMapping(webApp, servlet); + if (mapping == null) + { + mapping = WebFactory.eINSTANCE + .createServletMapping(); + mapping.setServletName(servlet.getServletName()); + webApp.getServletMappings().add(mapping); + } + // Add patterns + for (final String pattern : urlMappingList) + { + if (!(doesServletMappingPatternExist(webApp, servlet, pattern))) + { + UrlPatternType urlPattern = JavaeeFactory.eINSTANCE + .createUrlPatternType(); + urlPattern.setValue(pattern); + mapping.getUrlPatterns().add(urlPattern); + } + } + } + } + + /** + * Checks whether given mapping exists for the given servlet + * @param webApp + * @param servlet + * @param pattern + * @return true or false + */ + public static boolean doesServletMappingPatternExist(final WebApp webApp, + final Servlet servlet, final String pattern) + { + List mappings = webApp.getServletMappings(); + String servletName = servlet.getServletName(); + if (servletName != null) + { + servletName = servletName.trim(); + for (final ServletMapping mapping : mappings) + { + if (mapping != null && + mapping.getServletName() != null && + servletName.equals(mapping.getServletName().trim())) + { + for (final UrlPatternType urlPattern : mapping.getUrlPatterns()) + { + String patternTypeValue = urlPattern.getValue(); + if (patternTypeValue != null + && pattern.equals(patternTypeValue.trim())) + return true; + } + } + } + } + return false; + } + + /** + * Removes servlet-mappings for servlet using servlet-name for >= 2.5 WebModules. + * @param webApp + * @param servlet + */ + public static void removeURLMappings(final WebApp webApp, final Servlet servlet) { + List mappings = webApp.getServletMappings(); + String servletName = servlet.getServletName(); + if (servletName != null) { + servletName = servletName.trim(); + for (int i=mappings.size()-1;i>=0;--i){ + ServletMapping mapping = (ServletMapping)mappings.get(i); + if (mapping != null && + mapping.getServletName() != null && + mapping.getServletName().trim() + .equals(servletName)) { + mappings.remove(mapping); + } + } + } + } + + /** + * Removes servlet definition + * @param webApp + * @param servlet + */ + public static void removeServlet(final WebApp webApp, final Servlet servlet) { + webApp.getServlets().remove(servlet); + } + + /** + * Removes context-param + * @param webApp + * @param paramName + */ + public static void removeContextParam(final WebApp webApp, String paramName) { + Iterator it = webApp.getContextParams().iterator(); + while (it.hasNext()) { + ParamValue cp = (ParamValue) it.next(); + if (cp.getParamName().equals(paramName)) { + webApp.getContextParams().remove(cp); + break; + } + } + } + + /** + * Creates or updates context-param + * @param webApp + * @param paramName + * @param paramValue + */ + public static void setupContextParam(final WebApp webApp, String paramName, String paramValue) { + // if not default name and location, then add context param + ParamValue foundCP = null; + ParamValue cp = null; + boolean found = false; + // check to see if present + Iterator it = webApp.getContextParams().iterator(); + while (it.hasNext()) { + cp = (org.eclipse.jst.javaee.core.ParamValue) it.next(); + if (cp != null && + cp.getParamName()!= null && + cp.getParamName().trim().equals(paramName)) { + foundCP = cp; + found = true; + } + } + if (!found) { + ParamValue pv = JavaeeFactory.eINSTANCE.createParamValue(); + pv.setParamName(paramName); + pv.setParamValue(paramValue); + webApp.getContextParams().add(pv); + } else { + cp = foundCP; + if (cp.getParamValue().indexOf(paramValue) < 0) { + String curVal = cp.getParamValue(); + String val = paramValue; + if (curVal != null && !"".equals(curVal.trim())) { //$NON-NLS-1$ + val = curVal + ",\n" + val; //$NON-NLS-1$ + } + cp.setParamValue(val); + } + } + } + + /** + * @param webApp + * @param paramName + * @return context-param value or null if context-param is not found + */ + public static String getContextParam(final WebApp webApp, String paramName) + { + for (Iterator it = webApp.getContextParams().iterator(); it.hasNext();) + { + ParamValue cp = (ParamValue) it.next(); + if (cp != null && + cp.getParamName()!= null && + cp.getParamName().trim().equals(paramName)) { + return cp.getParamValue(); + } + } + return null; + } + + /** + * @param map + * @return extension from map. Will return null if file extension not found in url patterns. + */ + public static String getFileExtensionFromMap(final ServletMapping map) { + List urls = map.getUrlPatterns(); + for (Iterator it=urls.iterator();it.hasNext();){ + IPath extPath = new Path(((UrlPatternType)it.next()).getValue()); + if (extPath != null){ + String ext = extPath.getFileExtension(); + if (ext != null && !ext.equals("")) //$NON-NLS-1$ + return ext; + } + } + return null; + } + + /** + * @param map + * @return prefix mapping + */ + public static String getPrefixMapping(final ServletMapping map) { + List urls = map.getUrlPatterns(); + for (Iterator it=urls.iterator();it.hasNext();){ + IPath extPath = new Path(((UrlPatternType)it.next()).getValue()); + if (extPath != null){ + String ext = extPath.getFileExtension(); + if (ext == null){ + String lastSeg = extPath.lastSegment(); + if (lastSeg.equals("*")) //$NON-NLS-1$ + { + return extPath.removeLastSegments(1).toString(); + } + + return extPath.toString(); + } + } + } + return null; + } +} diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java index b23a0e601..3b5789d86 100644 --- a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java +++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java @@ -217,10 +217,11 @@ public final class JSFFacetInstallDelegate implements IDelegate { updater.addContextParam("org.apache.myfaces.PRETTY_HTML", "true", Messages.JSFFacetInstallDelegate_PrettyHtmlDescription); //$NON-NLS-1$ //$NON-NLS-2$ updater.addContextParam("org.apache.myfaces.DETECT_JAVASCRIPT", "false", null); //$NON-NLS-1$//$NON-NLS-2$ updater.addContextParam("org.apache.myfaces.AUTO_SCROLL", "true", Messages.JSFFacetInstallDelegate_AutoScrollDescription); //$NON-NLS-1$ //$NON-NLS-2$ - - updater.addServlet("faces", "org.apache.myfaces.webapp.MyFacesServlet", "1"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - updater.addServletMapping("faces", "org.apache.myfaces.webapp.MyFacesServlet", "*.jsf"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ - updater.addServletMapping("faces", "org.apache.myfaces.webapp.MyFacesServlet", "*.faces"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ + +// Following 3 lines disabled for https://bugs.eclipse.org/bugs/show_bug.cgi?id=317865 +// updater.addServlet("faces", "org.apache.myfaces.webapp.MyFacesServlet", "1"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ +// updater.addServletMapping("faces", "org.apache.myfaces.webapp.MyFacesServlet", "*.jsf"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ +// updater.addServletMapping("faces", "org.apache.myfaces.webapp.MyFacesServlet", "*.faces"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ updater.addListener("org.apache.myfaces.webapp.StartupServletContextListener"); //$NON-NLS-1$ @@ -256,10 +257,11 @@ public final class JSFFacetInstallDelegate implements IDelegate { updater.addContextParam("javax.faces.STATE_SAVING_METHOD", "client", Messages.JSFFacetInstallDelegate_StateSavingMethod); //$NON-NLS-1$//$NON-NLS-2$ updater.addContextParam("javax.servlet.jsp.jstl.fmt.localizationContext", "resources.application", null); //$NON-NLS-1$ //$NON-NLS-2$ updater.addListener("com.sun.faces.config.ConfigureListener"); //$NON-NLS-1$ - - updater.addServlet("Faces Servlet", "javax.faces.webapp.FacesServlet", "1"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ - updater.addServletMapping("Faces Servlet", "javax.faces.webapp.FacesServlet", "*.jsf"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ - updater.addServletMapping("Faces Servlet", "javax.faces.webapp.FacesServlet", "*.faces"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ + +//Following 3 lines disabled for https://bugs.eclipse.org/bugs/show_bug.cgi?id=317865 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=317868 +// updater.addServlet("Faces Servlet", "javax.faces.webapp.FacesServlet", "1"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ +// updater.addServletMapping("Faces Servlet", "javax.faces.webapp.FacesServlet", "*.jsf"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ +// updater.addServletMapping("Faces Servlet", "javax.faces.webapp.FacesServlet", "*.faces"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ } diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils.java index 22c3f72dc..933acf7df 100644 --- a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils.java +++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils.java @@ -241,16 +241,6 @@ public abstract class JSFUtils { * @param webApp */ public abstract void rollbackWebApp(Object webApp); - - - /** - * @param webAppObj - * @param resource - * @param existingURL - * @return the modified url path for the (possibly) jsf resource. - */ - public abstract IPath getFileUrlPath(Object webAppObj, IResource resource, - IPath existingURL); /** * @param fileExtension @@ -278,6 +268,25 @@ public abstract class JSFUtils { return true; } + /** + * @param webApp + * @return the default file extension from the context param. Default is + * "jsp" if no context param. + */ + protected String getDefaultSuffix(Object webApp) { + String contextParam = null; + if(isJavaEE(webApp)) { + contextParam = JEEUtils.getContextParam((org.eclipse.jst.javaee.web.WebApp) webApp, JSF_DEFAULT_SUFFIX_CONTEXT_PARAM); + } + else { + contextParam = J2EEUtils.getContextParam((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, JSF_DEFAULT_SUFFIX_CONTEXT_PARAM); + } + if(contextParam == null) { + return getDefaultDefaultSuffix(); + } + return normalizeSuffix(contextParam); + } + /** * @return the default value for the default mapping suffix */ @@ -393,4 +402,248 @@ public abstract class JSFUtils { } } + + /** + * Finds and returns a JSF Servlet definition, or null if servlet is not defined. + * + * @param webApp + * @return Servlet or null + */ + protected Object findJSFServlet(Object webApp) { + if(isJavaEE(webApp)) { + return JEEUtils.findServlet((org.eclipse.jst.javaee.web.WebApp) webApp, JSF_SERVLET_CLASS); + } + return J2EEUtils.findServlet((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, JSF_SERVLET_CLASS); + } + + /** + * Creates servlet reference in WebApp if not present or updates servlet + * name if found using the passed configuration. + * + * @param webApp + * @param config + * @param servlet + * @return Servlet servlet - if passed servlet was null, will return created + * servlet + */ + protected Object createOrUpdateServletRef(final Object webApp, + final IDataModel config, Object servlet) + { + String displayName = getDisplayName(config); + String className = getServletClassname(config); + if(isJavaEE(webApp)) { + return JEEUtils.createOrUpdateServletRef((org.eclipse.jst.javaee.web.WebApp) webApp, displayName, className, (org.eclipse.jst.javaee.web.Servlet) servlet); + } + return J2EEUtils.createOrUpdateServletRef((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, displayName, className, (org.eclipse.jst.j2ee.webapplication.Servlet) servlet); + } + + /** + * Creates servlet-mappings for the servlet for 2.5 WebModules or greated + * + * @param webApp + * @param urlMappingList + * - list of string values to be used in url-pattern for + * servlet-mapping + * @param servlet + */ + protected void setUpURLMappings(final Object webApp, + final List urlMappingList, final Object servlet) + { + if(isJavaEE(webApp)) { + JEEUtils.setUpURLMappings((org.eclipse.jst.javaee.web.WebApp) webApp, urlMappingList, (org.eclipse.jst.javaee.web.Servlet) servlet); + } + else { + J2EEUtils.setUpURLMappings((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, urlMappingList, (org.eclipse.jst.j2ee.webapplication.Servlet) servlet); + } + } + + /** + * Removes servlet-mappings for servlet using servlet-name for >= 2.5 WebModules. + * @param webApp + * @param servlet + */ + protected void removeURLMappings(final Object webApp, final Object servlet) { + if(isJavaEE(webApp)) { + JEEUtils.removeURLMappings((org.eclipse.jst.javaee.web.WebApp) webApp, (org.eclipse.jst.javaee.web.Servlet) servlet); + } + else { + J2EEUtils.removeURLMappings((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, (org.eclipse.jst.j2ee.webapplication.Servlet) servlet); + } + } + + /** + * Removes servlet definition + * @param webApp + * @param servlet + */ + protected void removeJSFServlet(final Object webApp, final Object servlet) { + if(isJavaEE(webApp)) { + JEEUtils.removeServlet((org.eclipse.jst.javaee.web.WebApp) webApp, (org.eclipse.jst.javaee.web.Servlet) servlet); + } + else { + J2EEUtils.removeServlet((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, (org.eclipse.jst.j2ee.webapplication.Servlet) servlet); + } + } + + /** + * Removes context-param + * @param webApp + */ + protected void removeJSFContextParams(final Object webApp) { + if(isJavaEE(webApp)) { + JEEUtils.removeContextParam((org.eclipse.jst.javaee.web.WebApp) webApp, JSF_CONFIG_CONTEXT_PARAM); + } + else { + J2EEUtils.removeContextParam((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, JSF_CONFIG_CONTEXT_PARAM); + } + } + + /** + * Creates or updates context-params + * @param webApp + * @param config + */ + protected void setupContextParams(final Object webApp, final IDataModel config) { + final String paramValue = config.getStringProperty(IJSFFacetInstallDataModelProperties.CONFIG_PATH); + if (paramValue != null && !paramValue.equals(JSF_DEFAULT_CONFIG_PATH)) { + if(isJavaEE(webApp)) { + JEEUtils.setupContextParam((org.eclipse.jst.javaee.web.WebApp) webApp, JSF_CONFIG_CONTEXT_PARAM, paramValue); + } + else { + J2EEUtils.setupContextParam((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, JSF_CONFIG_CONTEXT_PARAM, paramValue); + } + } + + } + + /** + * @param map + * @param webApp + * @return extension from map. Will return null if file extension not found + * in url patterns. + */ + protected String getFileExtensionFromMap(final Object webApp, final Object map) { + if(isJavaEE(webApp)) { + return JEEUtils.getFileExtensionFromMap((org.eclipse.jst.javaee.web.ServletMapping) map); + } + return J2EEUtils.getFileExtensionFromMap((org.eclipse.jst.j2ee.webapplication.ServletMapping) map); + } + + /** + * @param webApp + * @param map + * @return prefix mapping. may return null. + */ + protected String getPrefixMapping(final Object webApp, final Object map) { + if(isJavaEE(webApp)) { + return JEEUtils.getPrefixMapping((org.eclipse.jst.javaee.web.ServletMapping) map); + } + return J2EEUtils.getPrefixMapping((org.eclipse.jst.j2ee.webapplication.ServletMapping) map); + } + + /** + * @param webAppObj + * @param resource + * @param existingURL + * @return the modified url path for the (possibly) jsf resource. + */ + public IPath getFileUrlPath(Object webAppObj, IResource resource, + IPath existingURL) { + // if not a JSF page, do nothing + if (!isJSFPage(resource)) + { + return null; + } + + Object servlet = findJSFServlet(webAppObj); + if (servlet == null) + {// if no faces servlet, do nothing + return null; + } + + String defaultSuffix = getDefaultSuffix(webAppObj); + // is the resource using default_suffix + String fileExtension = resource.getFileExtension(); + boolean canUseExtensionMapping = fileExtension != null && fileExtension.equalsIgnoreCase(defaultSuffix); + // if not using default extension and is not a known file extension, + // then we will abort + if (!canUseExtensionMapping + && !isValidKnownExtension(resource.getFileExtension())) + return null; + + if(isJavaEE(webAppObj)) { + org.eclipse.jst.javaee.web.WebApp webApp = (org.eclipse.jst.javaee.web.WebApp) webAppObj; + + final String servletName = ((org.eclipse.jst.javaee.web.Servlet) servlet).getServletName(); + + String foundFileExtension = null; + for (final org.eclipse.jst.javaee.web.ServletMapping map : webApp.getServletMappings()) + { + if (map != null && + map.getServletName() != null && + map.getServletName().trim().equals(servletName.trim())) + { + foundFileExtension = getFileExtensionFromMap(webAppObj, map); + if (foundFileExtension != null && canUseExtensionMapping) + { + return existingURL.removeFileExtension() + .addFileExtension(foundFileExtension); + } + + String foundPrefixMapping = getPrefixMapping(webAppObj, map); + if (foundPrefixMapping != null) + { + return new Path(foundPrefixMapping).append(existingURL); + } + } + } + + if (!canUseExtensionMapping && foundFileExtension != null) + { + // we could prompt user that this may not work... + // for now we will return the extension mapping + return existingURL.removeFileExtension().addFileExtension( + foundFileExtension); + } + + // we could, at this point, add a url mapping to the faces servlet, + // or prompt user that it may be a good idea to add one... + + } + else { + Iterator mappings = ((org.eclipse.jst.j2ee.webapplication.Servlet)servlet).getMappings().iterator(); + org.eclipse.jst.j2ee.webapplication.ServletMapping map = null; + String foundFileExtension = null; + String foundPrefixMapping = null; + while (mappings.hasNext()) + { + map = (org.eclipse.jst.j2ee.webapplication.ServletMapping)mappings.next(); + + foundFileExtension = getFileExtensionFromMap(webAppObj, map); + if (foundFileExtension != null && canUseExtensionMapping) + { + return existingURL.removeFileExtension().addFileExtension(foundFileExtension); + } + + if (foundPrefixMapping == null) + { + foundPrefixMapping = getPrefixMapping(webAppObj, map); + } + } + if (foundPrefixMapping != null) + { + return new Path(foundPrefixMapping).append(existingURL); + } + if (! canUseExtensionMapping && foundFileExtension != null){ + //we could prompt user that this may not work... + //for now we will return the extension mapping + return existingURL.removeFileExtension().addFileExtension(foundFileExtension); + } + + // we could, at this point, add a url mapping to the faces servlet, + // or prompt user that it may be a good idea to add one... + + } + return null; + } } diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils11.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils11.java index 7ba1a5878..020d4fe4c 100644 --- a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils11.java +++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils11.java @@ -12,23 +12,9 @@ package org.eclipse.jst.jsf.core.internal.project.facet; import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; -import org.eclipse.jst.j2ee.common.CommonFactory; -import org.eclipse.jst.j2ee.common.ParamValue; -import org.eclipse.jst.j2ee.internal.J2EEVersionConstants; import org.eclipse.jst.j2ee.model.IModelProvider; -import org.eclipse.jst.j2ee.webapplication.ContextParam; -import org.eclipse.jst.j2ee.webapplication.Servlet; -import org.eclipse.jst.j2ee.webapplication.ServletMapping; -import org.eclipse.jst.j2ee.webapplication.ServletType; -import org.eclipse.jst.j2ee.webapplication.WebApp; -import org.eclipse.jst.j2ee.webapplication.WebapplicationFactory; import org.eclipse.jst.jsf.core.JSFVersion; import org.eclipse.wst.common.frameworks.datamodel.IDataModel; @@ -47,43 +33,6 @@ import org.eclipse.wst.common.frameworks.datamodel.IDataModel; super(JSFVersion.V1_1, modelProvider); } - /** - * @param webApp - * as Object - * @return Servlet - the JSF Servlet for the specified WebApp or null if not - * present - */ - private Servlet findJSFServlet(final WebApp webApp) - { - if (webApp == null) - { - return null; - } - - final Iterator it = webApp.getServlets().iterator(); - - while (it.hasNext()) - { - final Servlet servlet = (Servlet) it.next(); - if (servlet != null && servlet.getWebType() != null) - { - if (servlet.getWebType().isServletType()) - { - if (((ServletType) servlet.getWebType()).getClassName() != null - && ((ServletType) servlet.getWebType()) - .getClassName().trim().equals( - JSF_SERVLET_CLASS)) - { - return servlet; - } - } - } - } - - // if we get to here then we have finished the loop - // without finding the servlet we're looking for - return null; - } /** * Creates a stubbed JSF configuration file for specified JSF version and @@ -110,478 +59,40 @@ import org.eclipse.wst.common.frameworks.datamodel.IDataModel; pw.write("\n"); //$NON-NLS-1$ } - /** - * Creates servlet reference in WebApp if not present or updates servlet - * name if found using the passed configuration. - * - * @param webApp - * @param config - * @param servlet - * @return Servlet servlet - if passed servlet was null, will return created - * servlet - */ - private Servlet createOrUpdateServletRef(final WebApp webApp, - final IDataModel config, Servlet servlet) - { - - String displayName = getDisplayName(config); - String className = getServletClassname(config); - - if (servlet == null) - { - // Create the servlet instance and set up the parameters from data - // model - servlet = WebapplicationFactory.eINSTANCE.createServlet(); - servlet.setServletName(displayName); - - ServletType servletType = WebapplicationFactory.eINSTANCE - .createServletType(); - servletType.setClassName(className); - servlet.setWebType(servletType); - servlet.setLoadOnStartup(Integer.valueOf(1)); - // Add the servlet to the web application model - webApp.getServlets().add(servlet); - } - else - { - // update - updateServletMappings(webApp, servlet, displayName); - servlet.setServletName(displayName); - servlet.setLoadOnStartup(Integer.valueOf(1)); - } - return servlet; - } - - private void updateServletMappings(final WebApp webApp, - final Servlet servlet, final String displayName) - { - List mappings = findServletMappings(webApp, servlet); - for (ServletMapping map : mappings) - { - map.setName(displayName); - } - - } - - private List findServletMappings(final WebApp webApp, final Servlet servlet) { - String servletName = servlet.getServletName(); - servletName = servletName != null ? servletName.trim() : servletName; - final List mappings = new ArrayList(); - final List allMappings = webApp.getServletMappings(); - for (int i=allMappings.size()-1;i>=0;--i){ - ServletMapping mapping = allMappings.get(i); - if (mapping != null && - mapping.getServlet() != null && - mapping.getServlet().getServletName() != null && - mapping.getServlet().getServletName().trim().equals(servletName)) - mappings.add(mapping); - } - return mappings; - } - - /** - * Creates servlet-mappings for the servlet - * - * @param webApp - * @param urlMappingList - list of string values to be used in url-pattern for servlet-mapping - * @param servlet - */ - private void setUpURLMappings(final WebApp webApp, final List urlMappingList, - final Servlet servlet) { - // Add mappings - Iterator it = urlMappingList.iterator(); - while (it.hasNext()) { - String pattern = (String) it.next(); - if (!(doesServletMappingExist(webApp, servlet, pattern))){ - ServletMapping mapping = WebapplicationFactory.eINSTANCE - .createServletMapping(); - mapping.setServlet(servlet); - mapping.setName(servlet.getServletName()); - mapping.setUrlPattern(pattern); - webApp.getServletMappings().add(mapping); - } - } - } - - - private boolean doesServletMappingExist(final WebApp webApp, final Servlet servlet, - final String pattern) { - - List mappings = webApp.getServletMappings(); - String servletName = servlet.getServletName(); - if (servletName != null) - { - final Iterator it = mappings.iterator(); - while(it.hasNext()) - { - ServletMapping mapping = (ServletMapping) it.next(); - if (mapping != null && - mapping.getServlet() != null && - mapping.getServlet().getServletName() != null && - mapping.getServlet().getServletName().trim().equals(servletName) && - mapping.getUrlPattern() != null && - mapping.getUrlPattern().trim().equals(pattern)) { - return true; - } - } - } - return false; - } - - /** - * Removes servlet-mappings for servlet using servlet-name. - * @param webApp - * @param servlet - */ - private void removeURLMappings(final WebApp webApp, final Servlet servlet) { - List mappings = webApp.getServletMappings(); - String servletName = servlet.getServletName(); - if (servletName != null) { - for (int i=mappings.size()-1;i>=0;--i){ - ServletMapping mapping = (ServletMapping)mappings.get(i); - if (mapping != null && - mapping.getServlet() != null && - mapping.getServlet().getServletName() != null && - mapping.getServlet().getServletName().trim() - .equals(servletName)) { - mappings.remove(mapping); - } - } - } - } - - /** - * Creates or updates config file context-param in v 2.3 WebApp if non default configuration file is specified. - * @param webApp - * @param config - */ - private void setupConfigFileContextParamForV2_3(final WebApp webApp, - final IDataModel config) { - // if not default name and location, then add context param - ContextParam cp = null; - ContextParam foundCP = null; - boolean found = false; - final String stringProperty = - config.getStringProperty(IJSFFacetInstallDataModelProperties.CONFIG_PATH); - if (stringProperty != null && - !stringProperty.equals(JSF_DEFAULT_CONFIG_PATH)) { - // check to see if present - Iterator it = webApp.getContexts().iterator(); - while (it.hasNext()) { - cp = (ContextParam) it.next(); - if (cp != null && - cp.getParamName() != null && - cp.getParamName().equals(JSF_CONFIG_CONTEXT_PARAM)) { - foundCP = cp; - found = true; - } - } - if (!found) { - cp = WebapplicationFactory.eINSTANCE.createContextParam(); - cp.setParamName(JSF_CONFIG_CONTEXT_PARAM); - cp.setParamValue(stringProperty); - webApp.getContexts().add(cp); - } else { - cp = foundCP; - if (cp.getParamValue().indexOf(stringProperty) < 0) { - String curVal = cp.getParamValue(); - String val = stringProperty; - if (curVal != null && !"".equals(curVal.trim())) { //$NON-NLS-1$ - val = curVal + ",\n" + val; //$NON-NLS-1$ - } - cp.setParamValue(val); - } - } - } - } - /** - * Creates or updates config file context-param in v2.4 WebApp if non default configuration file is specified. - * @param webApp - * @param config - */ - private void setupConfigFileContextParamForV2_4(final WebApp webApp, - final IDataModel config) { - // if not default name and location, then add context param - ParamValue foundCP = null; - ParamValue cp = null; - boolean found = false; - final String stringProperty = - config.getStringProperty(IJSFFacetInstallDataModelProperties.CONFIG_PATH); - if (stringProperty != null && - !stringProperty.equals(JSF_DEFAULT_CONFIG_PATH)) { - // check to see if present - Iterator it = webApp.getContextParams().iterator(); - while (it.hasNext()) { - cp = (ParamValue) it.next(); - if (cp != null && - cp.getName() != null && - cp.getName().trim().equals(JSF_CONFIG_CONTEXT_PARAM)) { - foundCP = cp; - found = true; - } - } - if (!found) { - ParamValue pv = CommonFactory.eINSTANCE.createParamValue(); - pv.setName(JSF_CONFIG_CONTEXT_PARAM); - pv.setValue(stringProperty); - webApp.getContextParams().add(pv); - } else { - cp = foundCP; - if (cp.getValue().indexOf(stringProperty) < 0) { - String curVal = cp.getValue(); - String val = stringProperty; - if (curVal != null && !"".equals(curVal.trim())) { //$NON-NLS-1$ - val = curVal + ",\n" + val; //$NON-NLS-1$ - } - cp.setValue(val); - } - } - } - } - - /** - * @param webApp - * @return the default file extension from the context param. Default is - * "jsp" if no context param. - */ - private String getDefaultSuffix(final WebApp webApp) - { - String defaultSuffix = getDefaultDefaultSuffix(); - if ("2.3".equals(webApp.getVersion())) //$NON-NLS-1$ - { - for (Iterator it = webApp.getContexts().iterator(); it.hasNext();) - { - ContextParam cp = (ContextParam) it.next(); - if (cp != null) - { - final String paramName = cp.getParamName(); - final String suffix = calculateSuffix(paramName, cp - .getParamValue()); - if (suffix != null) - { - return suffix; - } - } - } - } - else if ("2.4".equals(webApp.getVersion())) //$NON-NLS-1$ - { - for (Iterator it = webApp.getContextParams().iterator(); it.hasNext();) - { - ParamValue cp = (ParamValue) it.next(); - if (cp != null) - { - final String paramName = cp.getName(); - final String suffix = calculateSuffix(paramName, cp - .getValue()); - if (suffix != null) - { - return suffix; - } - } - } - } - return defaultSuffix; - } - - /** - * @param map - * @return prefix mapping. may return null. - */ - private String getPrefixMapping(final ServletMapping map) - { - final String urlPattern = map.getUrlPattern(); - if (urlPattern != null && urlPattern.trim().length() != 0) - { - IPath extPath = new Path(urlPattern); - if (extPath != null) - { - String ext = extPath.getFileExtension(); - if (ext == null) - { - String lastSeg = extPath.lastSegment(); - if (lastSeg != null && lastSeg.equals("*")) //$NON-NLS-1$ - { - return extPath.removeLastSegments(1).toString(); - } - - return extPath.toString(); - } - } - } - return null; - } - - /** - * @param map - * @return extension from map. Will return null if file extension not found - * in url patterns. - */ - private String getFileExtensionFromMap(final ServletMapping map) - { - final String urlPattern = map.getUrlPattern(); - if (urlPattern != null - && urlPattern.trim().length() != 0) - { - IPath extPath = new Path(map.getUrlPattern()); - if (extPath != null) - { - String ext = extPath.getFileExtension(); - if (ext != null && ext.trim().length() != 0) - { - return ext; - } - } - } - return null; - } - @Override public void updateWebApp(Object webApp, IDataModel config) { // create or update servlet ref - Servlet servlet = findJSFServlet((WebApp) webApp);// check to see + Object servlet = findJSFServlet(webApp);// check to see // if already // present - servlet = createOrUpdateServletRef((WebApp) webApp, config, servlet); + servlet = createOrUpdateServletRef(webApp, config, servlet); // init mappings final List listOfMappings = getServletMappings(config); - setUpURLMappings((WebApp) webApp, listOfMappings, servlet); + setUpURLMappings(webApp, listOfMappings, servlet); // setup context params - setupContextParams((WebApp) webApp, config); - } - - private void setupContextParams(WebApp webApp, final IDataModel config) { - if (webApp.getVersionID() == J2EEVersionConstants.WEB_2_3_ID)//shouldn't have to do it this way, but that's the way it goes 119442 - { - setupConfigFileContextParamForV2_3(webApp, config); - } - else if (webApp.getVersionID() == J2EEVersionConstants.WEB_2_4_ID) - { - setupConfigFileContextParamForV2_4(webApp, config); - } - else - { - throw new IllegalArgumentException("Invalid argument: "+webApp.getVersionID()); //$NON-NLS-1$ - } + setupContextParams(webApp, config); } @Override public void rollbackWebApp(Object webApp) { - org.eclipse.jst.j2ee.webapplication.Servlet servlet = findJSFServlet((WebApp) webApp); + Object servlet = findJSFServlet(webApp); if (servlet == null) { return; } // remove faces url mappings - removeURLMappings((WebApp) webApp, servlet); + removeURLMappings(webApp, servlet); // remove context params - removeJSFContextParams((WebApp) webApp, servlet); + removeJSFContextParams(webApp); // remove servlet - removeJSFServlet((WebApp) webApp, servlet); - } - - private void removeJSFContextParams(final org.eclipse.jst.j2ee.webapplication.WebApp webApp, final org.eclipse.jst.j2ee.webapplication.Servlet servlet) { - if ("2.3".equals(webApp.getVersion())) //$NON-NLS-1$ - { - Iterator it = webApp.getContexts().iterator(); - while (it.hasNext()) - { - final ContextParam cp = (ContextParam) it.next(); - if (JSFUtils.JSF_CONFIG_CONTEXT_PARAM.equals(cp.getParamName())) - { - webApp.getContexts().remove(cp); - break; - } - } - } - else if ("2.4".equals(webApp.getVersion())) //$NON-NLS-1$ - { - Iterator it = webApp.getContextParams().iterator(); - while (it.hasNext()) - { - ParamValue cp = (ParamValue) it.next(); - if (cp.getName().equals(JSFUtils.JSF_CONFIG_CONTEXT_PARAM)) { - webApp.getContextParams().remove(cp); - break; - } - } - } - // otherwise do nothing - } - - private void removeJSFServlet(final org.eclipse.jst.j2ee.webapplication.WebApp webApp, final org.eclipse.jst.j2ee.webapplication.Servlet servlet) { - webApp.getServlets().remove(servlet); + removeJSFServlet(webApp, servlet); } - @Override - public IPath getFileUrlPath(Object webAppObj, IResource resource, - IPath existingURL) - { - if (webAppObj instanceof WebApp) - { - final WebApp webApp = (WebApp)webAppObj; - final Servlet servlet = findJSFServlet(webApp); - // if no faces servlet, do nothing - if (servlet == null) - { - return null; - } - - //if not a JSF page, do nothing - if (!isJSFPage(resource)) - { - return null; - } - - final String defaultSuffix = getDefaultSuffix(webApp); - //is the resource using default_suffix - final String fileExtension = resource.getFileExtension(); - final boolean canUseExtensionMapping = - fileExtension != null && fileExtension.equalsIgnoreCase(defaultSuffix); - - //if not using default extension and is not a known file extension, then we will abort - if (! canUseExtensionMapping && ! isValidKnownExtension(fileExtension)) - { - return null; - } - - Iterator mappings = servlet.getMappings().iterator(); - ServletMapping map = null; - String foundFileExtension = null; - String foundPrefixMapping = null; - while (mappings.hasNext()) - { - map = (ServletMapping)mappings.next(); - - foundFileExtension = getFileExtensionFromMap(map); - if (foundFileExtension != null && canUseExtensionMapping) - { - return existingURL.removeFileExtension().addFileExtension(foundFileExtension); - } - - if (foundPrefixMapping == null) - { - foundPrefixMapping = getPrefixMapping(map); - } - } - if (foundPrefixMapping != null) - { - return new Path(foundPrefixMapping).append(existingURL); - } - if (! canUseExtensionMapping && foundFileExtension != null){ - //we could prompt user that this may not work... - //for now we will return the extension mapping - return existingURL.removeFileExtension().addFileExtension(foundFileExtension); - } - - //we could, at this point, add a url mapping to the faces servlet, or prompt user that it may be a good idea to add one... ;- - } - return null; - } + } diff --git a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils12.java b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils12.java index 811a0eb87..bb3bd9609 100644 --- a/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils12.java +++ b/jsf/plugins/org.eclipse.jst.jsf.core/src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils12.java @@ -12,20 +12,9 @@ package org.eclipse.jst.jsf.core.internal.project.facet; import java.io.PrintWriter; -import java.util.Iterator; import java.util.List; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.IPath; -import org.eclipse.core.runtime.Path; import org.eclipse.jst.j2ee.model.IModelProvider; -import org.eclipse.jst.javaee.core.JavaeeFactory; -import org.eclipse.jst.javaee.core.ParamValue; -import org.eclipse.jst.javaee.core.UrlPatternType; -import org.eclipse.jst.javaee.web.Servlet; -import org.eclipse.jst.javaee.web.ServletMapping; -import org.eclipse.jst.javaee.web.WebApp; -import org.eclipse.jst.javaee.web.WebFactory; import org.eclipse.jst.jsf.core.JSFVersion; import org.eclipse.wst.common.frameworks.datamodel.IDataModel; @@ -59,34 +48,6 @@ import org.eclipse.wst.common.frameworks.datamodel.IDataModel; } } - /** - * @param webApp - * @return Servlet - the JSF Servlet for the specified WebApp or null if not - * present - */ - private Servlet findJSFServlet(final WebApp webApp) - { - if (webApp == null) - { - return null; - } - - for (final Servlet servlet : webApp.getServlets()) - { - if (servlet != null && - servlet.getServletClass() != null - && servlet.getServletClass().trim().equals( - JSF_SERVLET_CLASS)) - { - return servlet; - } - } - - // if we get to here then we have finished the loop - // without finding the servlet we're looking for - return null; - } - @Override public void doVersionSpecificConfigFile(PrintWriter pw) { @@ -109,375 +70,39 @@ import org.eclipse.wst.common.frameworks.datamodel.IDataModel; pw.write(" " + "version=" + QUOTE + getVersion().toString() + QUOTE + ">\n\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ pw.write("\n"); //$NON-NLS-1$ } - - /** - * Creates servlet reference in WebApp if not present or updates servlet name if found - * using the passed configuration. - * - * @param webApp - * @param config - * @param servlet - * @return Servlet servlet - if passed servlet was null, will return created servlet - */ - private Servlet createOrUpdateServletRef(final WebApp webApp, - final IDataModel config, Servlet servlet) { - - String displayName = getDisplayName(config); - String className = getServletClassname(config); - - if (servlet == null){ - // Create the servlet instance and set up the parameters from data - // model - servlet = WebFactory.eINSTANCE.createServlet(); - servlet.setServletName(displayName); - servlet.setServletClass(className); - servlet.setLoadOnStartup(Integer.valueOf(1)); - // Add the servlet to the web application model - webApp.getServlets().add(servlet); - - } else { - updateServletMappings(webApp, servlet, displayName); - servlet.setServletName(displayName); - servlet.setLoadOnStartup(Integer.valueOf(1)); - } - return servlet; - } - - private void updateServletMappings(final WebApp webApp, - final Servlet servlet, final String displayName) - { - // update mappings for new name - ServletMapping mapping = findServletMapping(webApp, servlet); - if (mapping != null) - { - mapping.setServletName(displayName); - } - } - - /** - * Creates servlet-mappings for the servlet for 2.5 WebModules or greated - * - * @param webApp - * @param urlMappingList - * - list of string values to be used in url-pattern for - * servlet-mapping - * @param servlet - */ - private void setUpURLMappings(final WebApp webApp, - final List urlMappingList, final Servlet servlet) - { - - if (urlMappingList.size() > 0) - { - ServletMapping mapping = findServletMapping(webApp, servlet); - if (mapping == null) - { - mapping = WebFactory.eINSTANCE - .createServletMapping(); - mapping.setServletName(servlet.getServletName()); - webApp.getServletMappings().add(mapping); - } - // Add patterns - for (final String pattern : urlMappingList) - { - if (!(doesServletMappingPatternExist(webApp, servlet, pattern))) - { - UrlPatternType urlPattern = JavaeeFactory.eINSTANCE - .createUrlPatternType(); - urlPattern.setValue(pattern); - mapping.getUrlPatterns().add(urlPattern); - } - } - } - } - - private ServletMapping findServletMapping(final WebApp webApp, final Servlet servlet) { - for (Iterator it=webApp.getServletMappings().iterator();it.hasNext();){ - ServletMapping mapping = (ServletMapping)it.next(); - if (mapping.getServletName() != null && - servlet.getServletName() != null && - mapping.getServletName().trim().equals(servlet.getServletName().trim())) - return mapping; - } - return null; - } - - private boolean doesServletMappingPatternExist(final WebApp webApp, - final Servlet servlet, final String pattern) - { - List mappings = webApp.getServletMappings(); - String servletName = servlet.getServletName(); - if (servletName != null) - { - servletName = servletName.trim(); - for (final ServletMapping mapping : mappings) - { - if (mapping != null && - mapping.getServletName() != null && - servletName.equals(mapping.getServletName().trim())) - { - for (final UrlPatternType urlPattern : mapping.getUrlPatterns()) - { - String patternTypeValue = urlPattern.getValue(); - if (patternTypeValue != null - && pattern.equals(patternTypeValue.trim())) - return true; - } - } - } - } - return false; - } - /** - * Removes servlet-mappings for servlet using servlet-name for >= 2.5 WebModules. - * @param webApp - * @param servlet - */ - private void removeURLMappings(final WebApp webApp, final Servlet servlet) { - List mappings = webApp.getServletMappings(); - String servletName = servlet.getServletName(); - if (servletName != null) { - servletName = servletName.trim(); - for (int i=mappings.size()-1;i>=0;--i){ - ServletMapping mapping = (ServletMapping)mappings.get(i); - if (mapping != null && - mapping.getServletName() != null && - mapping.getServletName().trim() - .equals(servletName)) { - mappings.remove(mapping); - } - } - } - } - - /** - * Creates or updates config file context-param in v2.5 WebApp if non default configuration file is specified. - * @param webApp - * @param config - */ - private void setupConfigFileContextParamForV2_5(final org.eclipse.jst.javaee.web.WebApp webApp, - final IDataModel config) { - // if not default name and location, then add context param - ParamValue foundCP = null; - ParamValue cp = null; - boolean found = false; - String stringProperty = config.getStringProperty(IJSFFacetInstallDataModelProperties.CONFIG_PATH); - if (stringProperty != null && - !stringProperty.equals(JSF_DEFAULT_CONFIG_PATH)) { - // check to see if present - Iterator it = webApp.getContextParams().iterator(); - while (it.hasNext()) { - cp = (org.eclipse.jst.javaee.core.ParamValue) it.next(); - if (cp != null && - cp.getParamName()!= null && - cp.getParamName().trim().equals(JSF_CONFIG_CONTEXT_PARAM)) { - foundCP = cp; - found = true; - } - } - if (!found) { - ParamValue pv = JavaeeFactory.eINSTANCE.createParamValue(); - pv.setParamName(JSF_CONFIG_CONTEXT_PARAM); - pv.setParamValue(stringProperty); - webApp.getContextParams().add(pv); - } else { - cp = foundCP; - if (cp.getParamValue().indexOf(stringProperty) < 0) { - String curVal = cp.getParamValue(); - String val = stringProperty; - if (curVal != null && !"".equals(curVal.trim())) { //$NON-NLS-1$ - val = curVal + ",\n" + val; //$NON-NLS-1$ - } - cp.setParamValue(val); - } - } - } - } - - /** - * @param webApp - * @return the default file extension from the context param. Default is - * "jsp" if no context param - */ - private String getDefaultSuffix(final WebApp webApp) - { - String defaultSuffix = getDefaultDefaultSuffix(); - for (Iterator it = webApp.getContextParams().iterator(); it.hasNext();) - { - ParamValue cp = (ParamValue) it.next(); - if (cp != null) - { - final String paramName = cp.getParamName(); - final String suffix = calculateSuffix(paramName, cp - .getParamValue()); - if (suffix != null) - { - return suffix; - } - } - } - return defaultSuffix; - } - - - /** - * @param map - * @return prefix mapping - */ - private String getPrefixMapping(final ServletMapping map) { - List urls = map.getUrlPatterns(); - for (Iterator it=urls.iterator();it.hasNext();){ - IPath extPath = new Path(((UrlPatternType)it.next()).getValue()); - if (extPath != null){ - String ext = extPath.getFileExtension(); - if (ext == null){ - String lastSeg = extPath.lastSegment(); - if (lastSeg.equals("*")) //$NON-NLS-1$ - { - return extPath.removeLastSegments(1).toString(); - } - - return extPath.toString(); - } - } - } - return null; - } - - /** - * @param map - * @return extension from map. Will return null if file extension not found in url patterns. - */ - private String getFileExtensionFromMap(final ServletMapping map) { - List urls = map.getUrlPatterns(); - for (Iterator it=urls.iterator();it.hasNext();){ - IPath extPath = new Path(((UrlPatternType)it.next()).getValue()); - if (extPath != null){ - String ext = extPath.getFileExtension(); - if (ext != null && !ext.equals("")) //$NON-NLS-1$ - return ext; - } - } - return null; - } - @Override public void updateWebApp(Object webApp, IDataModel config) { // create or update servlet ref - Servlet servlet = findJSFServlet((WebApp)webApp);// check to see + Object servlet = findJSFServlet(webApp);// check to see // if already - servlet = createOrUpdateServletRef((WebApp) webApp, config, servlet); + servlet = createOrUpdateServletRef(webApp, config, servlet); // init mappings final List listOfMappings = getServletMappings(config); - setUpURLMappings((WebApp)webApp, listOfMappings, servlet); + setUpURLMappings(webApp, listOfMappings, servlet); // setup context params - setupConfigFileContextParamForV2_5((WebApp)webApp, config); - + setupContextParams(webApp, config); } @Override public void rollbackWebApp(Object webApp) { - Servlet servlet = findJSFServlet((WebApp) webApp); + Object servlet = findJSFServlet(webApp); if (servlet == null) { return; } // remove faces url mappings - removeURLMappings((WebApp)webApp, servlet); + removeURLMappings(webApp, servlet); // remove context params - removeJSFContextParams((WebApp)webApp, servlet); + removeJSFContextParams(webApp); // remove servlet - removeJSFServlet((WebApp)webApp, servlet); - } - - private void removeJSFContextParams(final WebApp webApp, final Servlet servlet) { - Iterator it = webApp.getContextParams().iterator(); - while (it.hasNext()) { - ParamValue cp = (ParamValue) it.next(); - if (cp.getParamName().equals(JSFUtils.JSF_CONFIG_CONTEXT_PARAM)) { - webApp.getContextParams().remove(cp); - break; - } - } - } - - private void removeJSFServlet(final WebApp webApp, final Servlet servlet) { - webApp.getServlets().remove(servlet); - } - - @Override - public IPath getFileUrlPath(Object webAppObj, IResource resource, - IPath existingURL) - { - if (webAppObj instanceof WebApp) - { - WebApp webApp = (WebApp) webAppObj; - Servlet servlet = findJSFServlet(webApp); - if (servlet == null) - {// if no faces servlet, do nothing - return null; - } - - final String servletName = servlet.getServletName(); - - // if not a JSF page, do nothing - if (!isJSFPage(resource)) - { - return null; - } - - String defaultSuffix = getDefaultSuffix(webApp); - // is the resource using default_suffix - String fileExtension = resource.getFileExtension(); - boolean canUseExtensionMapping = fileExtension != null && fileExtension.equalsIgnoreCase(defaultSuffix); - - // if not using default extension and is not a known file extension, - // then we will abort - if (!canUseExtensionMapping - && !isValidKnownExtension(resource.getFileExtension())) - return null; - - String foundFileExtension = null; - for (final ServletMapping map : webApp.getServletMappings()) - { - if (map != null && - map.getServletName() != null && - map.getServletName().trim().equals(servletName.trim())) - { - foundFileExtension = getFileExtensionFromMap(map); - if (foundFileExtension != null && canUseExtensionMapping) - { - return existingURL.removeFileExtension() - .addFileExtension(foundFileExtension); - } - - String foundPrefixMapping = getPrefixMapping(map); - if (foundPrefixMapping != null) - { - return new Path(foundPrefixMapping).append(existingURL); - } - } - } - - if (!canUseExtensionMapping && foundFileExtension != null) - { - // we could prompt user that this may not work... - // for now we will return the extension mapping - return existingURL.removeFileExtension().addFileExtension( - foundFileExtension); - } - - // we could, at this point, add a url mapping to the faces servlet, - // or prompt user that it may be a good idea to add one... ;- - } - return null; + removeJSFServlet(webApp, servlet); } -- cgit v1.2.3