Skip to main content
summaryrefslogtreecommitdiffstats
blob: 811a0eb87c21bef1c94dae46ceb3ff3630ce0e78 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*******************************************************************************
 * Copyright (c) 2005, 2008 Oracle Corporation.
 * 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:
 *    Gerry Kessler - initial API and implementation
 *******************************************************************************/ 

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;

/**
 * Utility file for JSF v1.2 model
 * 
 * @author Gerry Kessler - Oracle
 */
/*package: use JSFUtilFactory*/ class JSFUtils12 extends JSFUtils {

	/**
	 * @param modelProvider 
	 * 
	 */
	protected JSFUtils12(final IModelProvider modelProvider)
    {
        this(JSFVersion.V1_2, modelProvider);
    }

    /**
     * @param jsfVersion
     * @param modelProvider 
     */
    protected JSFUtils12(final JSFVersion jsfVersion, final IModelProvider modelProvider)
    {
        super(jsfVersion, modelProvider);
        if (jsfVersion.compareTo(JSFVersion.V1_2) < 0)
        {
            throw new IllegalArgumentException(
                    "JsfVersion must be at least 1.2"); //$NON-NLS-1$
        }
    }

    /**
     * @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)
    {
        final String QUOTE = new String(new char[]
        { '"' });
        final String schemaVersionString = getVersion().toString().replaceAll("\\.", "_");  //$NON-NLS-1$//$NON-NLS-2$
        pw.write("<?xml version=" + QUOTE + "1.0" + QUOTE + " encoding=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                + QUOTE + "UTF-8" + QUOTE + "?>\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
        pw.write("<faces-config\n"); //$NON-NLS-1$
        pw.write("    " + "xmlns=" + QUOTE //$NON-NLS-1$ //$NON-NLS-2$
                + "http://java.sun.com/xml/ns/javaee" + QUOTE + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
        pw.write("    " + "xmlns:xsi=" + QUOTE //$NON-NLS-1$ //$NON-NLS-2$
                + "http://www.w3.org/2001/XMLSchema-instance" + QUOTE //$NON-NLS-1$
                + "\n"); //$NON-NLS-1$
        pw.write("    " //$NON-NLS-1$
                + "xsi:schemaLocation=" //$NON-NLS-1$
                + QUOTE
                + String.format("http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_%s.xsd", schemaVersionString) //$NON-NLS-1$
                + QUOTE + "\n"); //$NON-NLS-1$
        pw.write("    " + "version=" + QUOTE + getVersion().toString() + QUOTE + ">\n\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        pw.write("</faces-config>\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<String> 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<ServletMapping> 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
                                                            // if already
        
        servlet = createOrUpdateServletRef((WebApp) webApp, config, servlet);

        // init mappings
        final List listOfMappings = getServletMappings(config);
        setUpURLMappings((WebApp)webApp, listOfMappings, servlet);

        // setup context params
        setupConfigFileContextParamForV2_5((WebApp)webApp, config);

    }

    
    @Override
    public void rollbackWebApp(Object webApp)
    {
        Servlet servlet = findJSFServlet((WebApp) webApp);
        if (servlet == null)
        {
            return;
        }
        // remove faces url mappings
        removeURLMappings((WebApp)webApp, servlet);
        // remove context params
        removeJSFContextParams((WebApp)webApp, servlet);
        // 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;
    }
    
    
}

Back to the top