001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.jsfsupport.container;
042:
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.security.Principal;
048: import java.util.Enumeration;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.Locale;
052: import java.util.Map;
053: import java.util.Set;
054: import javax.faces.context.ExternalContext; //import javax.portlet.PortletContext;
055: import javax.servlet.ServletContext;
056: import javax.servlet.http.HttpServletRequest;
057: import javax.servlet.http.HttpSession;
058: import org.openide.util.Enumerations;
059:
060: /**
061: * <p>Provides a JSF <code>ExternalContext</code> object for design-time use</p>
062: *
063: * @author Robert Brewin
064: * @version 1.0
065: */
066: public class RaveExternalContext extends ExternalContext {
067:
068: /**
069: * Holds the ServletContext or PortletContext
070: */
071: private Object context;
072:
073: /**
074: * Provides a session map for this mock external context
075: */
076: private HashMap sessionMap = new HashMap();
077:
078: /**
079: * Provides a request map if needed for this mock external context
080: */
081: private HashMap requestMap = new HashMap();
082:
083: /**
084: * Provides a application map if needed for this mock external context
085: */
086: private HashMap applicationMap = new HashMap();
087:
088: /**
089: * Provides a parameter map if needed for this mock external context
090: */
091: private HashMap requestParameterMap = new HashMap();
092:
093: /**
094: * Provides an init parameter map if needed for this mock external context.
095: */
096: private HashMap initParameterMap = new HashMap();
097:
098: /**
099: * empty map for stubbed out returns
100: * XXX Should I make it readonly, since it's shared for multiple types?
101: * (in case code tries to insert and remove items from these maps)
102: */
103: private HashMap emptyMap = new HashMap();
104:
105: /**
106: * Create a mock "external" context for the JSF container (example: a mock servlet context).
107: * This particular constructor initializes the servlet context with the passed in parameter
108: *
109: * @param context -- the context to bind to
110: */
111: public RaveExternalContext(Object context) {
112: this .context = context;
113: if (context instanceof ServletContext) {
114: ServletContext servletContext = (ServletContext) context;
115: Enumeration enumer = servletContext.getInitParameterNames();
116: while (enumer.hasMoreElements()) {
117: String name = (String) enumer.nextElement();
118: initParameterMap.put(name, servletContext
119: .getInitParameter(name));
120: }
121: }
122: // else if (context instanceof PortletContext) {
123: // PortletContext portletContext = (PortletContext) context;
124: // Enumeration enumer = portletContext.getInitParameterNames();
125: // while (enumer.hasMoreElements()) {
126: // String name = (String) enumer.nextElement();
127: // initParameterMap.put(name, portletContext.getInitParameter(name));
128: // }
129: // }
130: }
131:
132: /**
133: * <p>Dispatch a request to the apropriate context. In the case of servlets, this is done via
134: * "forward", but for portlets, it must use "include".</p>
135: */
136: public void dispatch(String path) throws IOException {
137: // TODO: Make this functional if needed
138: }
139:
140: /**
141: *
142: */
143: public String encodeActionURL(String url) {
144: // TODO: Make this functional if needed
145: return url;
146: }
147:
148: /**
149: *
150: */
151: public String encodeNamespace(String name) {
152: // TODO: Make this functional if needed
153: return name;
154: }
155:
156: public String encodeResourceURL(String url) {
157: return encodeActionURL(url);
158: }
159:
160: /**
161: *
162: */
163: public Map getApplicationMap() {
164: return applicationMap;
165: }
166:
167: /**
168: * @return
169: */
170: public String getAuthType() {
171: return null;
172: }
173:
174: /**
175: *
176: */
177: public Object getContext() {
178: return context;
179: }
180:
181: /**
182: *
183: */
184: public String getInitParameter(String name) {
185: return (String) initParameterMap.get(name);
186: }
187:
188: /**
189: *
190: */
191: public Map getInitParameterMap() {
192: return initParameterMap;
193: }
194:
195: /**
196: *
197: */
198: public String getRemoteUser() {
199: return null;
200: }
201:
202: /**
203: *
204: */
205: public Object getRequest() {
206: if (request == null) {
207: request = new DesigntimeServletRequest();
208: }
209: return request;
210: }
211:
212: DesigntimeServletRequest request;
213:
214: /** Designtime ServletRequest object, returns dummy data */
215: private class DesigntimeServletRequest implements
216: HttpServletRequest {
217: public Object getAttribute(String str) {
218: return null;
219: }
220:
221: public java.util.Enumeration getAttributeNames() {
222: return Enumerations.empty();
223: }
224:
225: public String getAuthType() {
226: return HttpServletRequest.BASIC_AUTH;
227: }
228:
229: public String getCharacterEncoding() {
230: // From javadoc: "This method returns null if the request does not specify a character encoding"
231: return null;
232: }
233:
234: public int getContentLength() {
235: return -1; // not known
236: }
237:
238: public String getContentType() {
239: return null; // content type not known
240: }
241:
242: public String getContextPath() {
243: return "/";
244: }
245:
246: public javax.servlet.http.Cookie[] getCookies() {
247: return new javax.servlet.http.Cookie[0];
248: }
249:
250: public long getDateHeader(String str) {
251: return System.currentTimeMillis();
252: }
253:
254: public String getHeader(String str) {
255: return ""; // NOI18N
256: }
257:
258: public java.util.Enumeration getHeaderNames() {
259: return Enumerations.empty();
260: }
261:
262: public java.util.Enumeration getHeaders(String str) {
263: return Enumerations.empty();
264: }
265:
266: public javax.servlet.ServletInputStream getInputStream()
267: throws IOException {
268: return null;
269: }
270:
271: public int getIntHeader(String str) {
272: return 0;
273: }
274:
275: public String getLocalAddr() {
276: return ""; // NOI18N
277: }
278:
279: public String getLocalName() {
280: return "127.0.0.1"; // NOI18N
281: }
282:
283: public int getLocalPort() {
284: return 8080; // if they try connecting back we shouldn't do this...
285: }
286:
287: public Locale getLocale() {
288: return Locale.getDefault();
289: }
290:
291: public java.util.Enumeration getLocales() {
292: return Enumerations.array(Locale.getAvailableLocales());
293: }
294:
295: public String getMethod() {
296: return "GET"; // NOI18N
297: }
298:
299: public String getParameter(String str) {
300: return null;
301: }
302:
303: public Map getParameterMap() {
304: return emptyMap;
305: }
306:
307: public java.util.Enumeration getParameterNames() {
308: return Enumerations.empty();
309: }
310:
311: public String[] getParameterValues(String str) {
312: return null;
313: }
314:
315: public String getPathInfo() {
316: return null;
317: }
318:
319: public String getPathTranslated() {
320: return null;
321: }
322:
323: public String getProtocol() {
324: return "HTTP/1.1"; // NOI18N
325: }
326:
327: public String getQueryString() {
328: return null;
329: }
330:
331: public java.io.BufferedReader getReader() throws IOException {
332: return null;
333: }
334:
335: public String getRealPath(String str) {
336: return null; // deprecated anyway
337: }
338:
339: public String getRemoteAddr() {
340: return "127.0.0.1"; // NOI18N
341: }
342:
343: public String getRemoteHost() {
344: return "localhost"; // NOI18N
345: }
346:
347: public int getRemotePort() {
348: return 8080;
349: }
350:
351: public String getRemoteUser() {
352: return "Creator"; // NOI18N
353: }
354:
355: public javax.servlet.RequestDispatcher getRequestDispatcher(
356: String str) {
357: return null;
358: }
359:
360: public String getRequestURI() {
361: return "/"; // XXX what should we return here?
362: }
363:
364: public StringBuffer getRequestURL() {
365: return new StringBuffer(); // XXX what should we return here?
366: }
367:
368: public String getRequestedSessionId() {
369: return null;
370: }
371:
372: public String getScheme() {
373: return "http"; // XXX or https?
374: }
375:
376: public String getServerName() {
377: return "localhost"; // NOI18N
378: }
379:
380: public int getServerPort() {
381: return 8080;
382: }
383:
384: public String getServletPath() {
385: return ""; // NOI18N
386: }
387:
388: public javax.servlet.http.HttpSession getSession() {
389: return getSession(true);
390: }
391:
392: public HttpSession getSession(boolean create) {
393: return (HttpSession) RaveExternalContext.this
394: .getSession(create);
395: }
396:
397: public Principal getUserPrincipal() {
398: return null;
399: }
400:
401: public boolean isRequestedSessionIdFromCookie() {
402: return true;
403: }
404:
405: public boolean isRequestedSessionIdFromURL() {
406: return false;
407: }
408:
409: public boolean isRequestedSessionIdFromUrl() {
410: return true;
411: }
412:
413: public boolean isRequestedSessionIdValid() {
414: return false;
415: }
416:
417: public boolean isSecure() {
418: return false;
419: }
420:
421: public boolean isUserInRole(String str) {
422: return false;
423: }
424:
425: public void removeAttribute(String str) {
426: }
427:
428: public void setAttribute(String str, Object obj) {
429: }
430:
431: public void setCharacterEncoding(String str)
432: throws java.io.UnsupportedEncodingException {
433: }
434: };
435:
436: /**
437: *
438: */
439: public String getRequestContextPath() {
440: // TODO: Make this functional if needed
441: return "";
442: }
443:
444: /**
445: *
446: */
447: public Map getRequestCookieMap() {
448: // TODO: Make this functional if needed
449: return emptyMap;
450: }
451:
452: /**
453: *
454: */
455: public Map getRequestHeaderMap() {
456: // Provide fake user agent string
457: if (headerMap == null) {
458: headerMap = new HashMap();
459: String userAgent = System.getProperty("rave.userAgent");
460: if (userAgent == null) {
461: //userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
462: userAgent = "Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.0.1) Gecko/20020920 Netscape/7.0";
463: }
464: headerMap.put("USER-AGENT", userAgent);
465: }
466: return headerMap;
467: }
468:
469: private HashMap headerMap;
470:
471: /**
472: *
473: */
474: public Map getRequestHeaderValuesMap() {
475: // TODO: Make this functional if needed
476: return emptyMap;
477: }
478:
479: /**
480: *
481: */
482: public Locale getRequestLocale() {
483: // TODO: Make this functional if needed
484: return null;
485: }
486:
487: static class EmptyIterator implements Iterator {
488: public boolean hasNext() {
489: return false;
490: }
491:
492: public Object next() {
493: return null;
494: }
495:
496: public void remove() {
497: }
498: }
499:
500: /**
501: *
502: */
503: public Iterator getRequestLocales() {
504: // TODO: Make this functional if needed
505: return new EmptyIterator();
506: }
507:
508: /**
509: *
510: */
511: public Map getRequestMap() {
512: return requestMap;
513: }
514:
515: /**
516: *
517: */
518: public Map getRequestParameterMap() {
519: return requestParameterMap;
520: }
521:
522: /**
523: *
524: */
525: public Iterator getRequestParameterNames() {
526: return requestParameterMap.keySet().iterator();
527: }
528:
529: /**
530: *
531: */
532: public Map getRequestParameterValuesMap() {
533: // TODO: Make this functional if needed
534: return emptyMap;
535: }
536:
537: /**
538: *
539: */
540: public String getRequestPathInfo() {
541: // TODO: Make this functional if needed
542: return "";
543: }
544:
545: /**
546: *
547: */
548: public String getRequestServletPath() {
549: // TODO: Make this functional if needed
550: return "";
551: }
552:
553: /**
554: *
555: */
556: public URL getResource(String path) throws MalformedURLException {
557: // TODO: Make this functional if needed
558: return null;
559: }
560:
561: /**
562: *
563: */
564: public InputStream getResourceAsStream(String path) {
565: // TODO: Make this functional if needed
566: return null;
567: }
568:
569: /**
570: *
571: */
572: public Set getResourcePaths(String path) {
573: // TODO: Make this functional if needed
574: return null;
575: }
576:
577: /**
578: *
579: */
580: public Object getResponse() {
581: // TODO: Make this functional if needed
582: return null;
583: }
584:
585: /**
586: *
587: */
588: public Object getSession(boolean create) {
589: // TODO: Make this functional if needed
590: return null;
591: }
592:
593: /**
594: * @return a <code>Map</code> that wraps the HttpSession's attribute set.
595: */
596: public Map getSessionMap() {
597: return sessionMap;
598: }
599:
600: /**
601: * @return
602: */
603: public Principal getUserPrincipal() {
604: return null;
605: }
606:
607: /**
608: * @return
609: */
610: public boolean isUserInRole(String role) {
611: return true;
612: }
613:
614: /**
615: *
616: */
617: public void log(String s) {
618: }
619:
620: /**
621: *
622: */
623: public void log(String s, Throwable t) {
624: }
625:
626: /**
627: *
628: */
629: public void redirect(String url) throws IOException {
630: }
631: }
|