1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.component.html.util;
20
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URLEncoder;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.TreeSet;
28
29 import javax.faces.FacesException;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import javax.servlet.ServletContext;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.apache.commons.lang.builder.EqualsBuilder;
37 import org.apache.commons.lang.builder.HashCodeBuilder;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.myfaces.component.html.util.StreamingThreadManager.HeaderInfoEntry;
41 import org.apache.myfaces.renderkit.html.util.AddResource;
42 import org.apache.myfaces.renderkit.html.util.AddResource2;
43 import org.apache.myfaces.renderkit.html.util.MyFacesResourceHandler;
44 import org.apache.myfaces.renderkit.html.util.ResourceHandler;
45 import org.apache.myfaces.renderkit.html.util.ResourceLoader;
46 import org.apache.myfaces.renderkit.html.util.ResourcePosition;
47 import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
48 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
49 import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
50 import org.apache.myfaces.tomahawk.util.ExternalContextUtils;
51 import org.apache.myfaces.webapp.filter.PortletUtils;
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 public class StreamingAddResource extends AddResource2
126 {
127
128
129
130
131
132
133
134
135 private static long REQUEST_ID_COUNTER = 0;
136
137
138
139
140 private Long requestId;
141
142
143
144
145 private HeaderInfoEntry headerInfoEntry;
146
147
148
149
150 private Set alreadySeenResources = new TreeSet();
151
152 private static final String PATH_SEPARATOR = "/";
153
154 protected static final Log log = LogFactory.getLog(StreamingAddResource.class);
155 protected static final Log logSend = LogFactory.getLog(StreamingAddResource.class.getName() + ".SEND");
156
157 private static final String RESOURCE_VIRTUAL_PATH = "/faces/myFacesExtensionResource";
158
159 private static final String RESOURCES_CACHE_KEY = AddResource.class.getName() + ".CACHE_KEY";
160
161 protected String _contextPath;
162 private String resourceVirtualPath;
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 public StreamingAddResource()
272 {
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 public void setContextPath(String contextPath)
294 {
295 _contextPath = contextPath;
296 }
297
298
299
300
301
302
303
304
305
306
307 public void addJavaScriptHere(FacesContext context, Class myfacesCustomComponent,
308 String resourceName) throws IOException
309 {
310 addJavaScriptHere(context, new MyFacesResourceHandler(myfacesCustomComponent, resourceName));
311 }
312
313
314
315
316
317
318
319 public void addJavaScriptHere(FacesContext context, String uri) throws IOException
320 {
321 ResponseWriter writer = context.getResponseWriter();
322
323 writer.startElement(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_ELEM, null);
324 writer.writeAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_TYPE_ATTR, org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
325 String src = context.getExternalContext().encodeResourceURL(getResourceUri(context, uri));
326 writer.writeURIAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SRC_ATTR, src, null);
327 writer.endElement(HTML.SCRIPT_ELEM);
328 }
329
330 public void addJavaScriptHerePlain(FacesContext context, String uri) throws IOException
331 {
332 ResponseWriter writer = context.getResponseWriter();
333
334 writer.startElement(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_ELEM, null);
335 writer.writeAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_TYPE_ATTR, org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
336 String src = getResourceUri(context, uri);
337 writer.writeURIAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SRC_ATTR, src, null);
338 writer.endElement(HTML.SCRIPT_ELEM);
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352 public void addJavaScriptHere(FacesContext context, ResourceHandler resourceHandler)
353 throws IOException
354 {
355 validateResourceHandler(resourceHandler);
356
357 ResponseWriter writer = context.getResponseWriter();
358
359 writer.startElement(HTML.SCRIPT_ELEM, null);
360 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
361 String src = context.getExternalContext().encodeResourceURL(
362 getResourceUri(context, resourceHandler));
363 writer.writeURIAttribute(HTML.SRC_ATTR, src, null);
364 writer.endElement(HTML.SCRIPT_ELEM);
365 }
366
367 public void addResourceHere(FacesContext context, ResourceHandler resourceHandler)
368 throws IOException
369 {
370 validateResourceHandler(resourceHandler);
371
372 String path = getResourceUri(context, resourceHandler);
373 ResponseWriter writer = context.getResponseWriter();
374 writer.write(context.getExternalContext().encodeResourceURL(path));
375 }
376
377
378
379
380
381
382
383
384
385 protected void validateResourceHandler(ResourceHandler resourceHandler)
386 {
387 if (resourceHandler == null)
388 {
389 throw new IllegalArgumentException("ResourceHandler is null");
390 }
391 validateResourceLoader(resourceHandler.getResourceLoaderClass());
392 }
393
394
395
396
397
398
399
400 protected void validateResourceLoader(Class resourceloader)
401 {
402 if (!ResourceLoader.class.isAssignableFrom(resourceloader))
403 {
404 throw new FacesException("Class " + resourceloader.getName() + " must implement "
405 + ResourceLoader.class.getName());
406 }
407 }
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425 public void addJavaScriptAtPosition(FacesContext context, ResourcePosition position,
426 ResourceHandler resourceHandler)
427 {
428 addJavaScriptAtPosition(context, position, resourceHandler, false);
429 }
430
431
432
433
434
435
436
437
438
439
440
441
442
443 public void addJavaScriptAtPosition(FacesContext context, ResourcePosition position,
444 Class myfacesCustomComponent, String resourceName)
445 {
446 addJavaScriptAtPosition(context, position, new MyFacesResourceHandler(
447 myfacesCustomComponent, resourceName));
448 }
449
450 public void addJavaScriptAtPositionPlain(FacesContext context, ResourcePosition position, Class myfacesCustomComponent, String resourceName)
451 {
452 addJavaScriptAtPosition(context, position,
453 new MyFacesResourceHandler(myfacesCustomComponent, resourceName),
454 false, false);
455 }
456
457
458
459
460
461
462
463
464
465
466
467 public void addJavaScriptAtPosition(FacesContext context, ResourcePosition position,
468 Class myfacesCustomComponent, String resourceName, boolean defer)
469 {
470 addJavaScriptAtPosition(context, position, new MyFacesResourceHandler(
471 myfacesCustomComponent, resourceName), defer);
472 }
473
474
475
476
477
478
479
480
481
482 public void addJavaScriptAtPosition(FacesContext context, ResourcePosition position, String uri)
483 {
484 addJavaScriptAtPosition(context, position, uri, false);
485 }
486
487
488
489
490
491 public void addJavaScriptAtPosition(FacesContext context, ResourcePosition position, String uri,
492 boolean defer)
493 {
494 WritablePositionedInfo info = (WritablePositionedInfo) getScriptInstance(context, uri, defer);
495 if (checkAlreadyAdded(info))
496 {
497 return;
498 }
499 HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
500 try
501 {
502 info.writePositionedInfo(response, context.getResponseWriter());
503 }
504 catch (IOException e)
505 {
506 throw new RuntimeException(e);
507 }
508 }
509
510 public void addJavaScriptToBodyTag(FacesContext context, String javascriptEventName,
511 String addedJavaScript)
512 {
513 throw new UnsupportedOperationException();
514 }
515
516
517
518
519
520 public void addJavaScriptAtPosition(FacesContext context, ResourcePosition position, ResourceHandler resourceHandler, boolean defer)
521 {
522 addJavaScriptAtPosition(context, position, resourceHandler, defer, false);
523 }
524
525 private void addJavaScriptAtPosition(FacesContext context, ResourcePosition position,
526 ResourceHandler resourceHandler, boolean defer, boolean encodeURL)
527 {
528 validateResourceHandler(resourceHandler);
529 WritablePositionedInfo info = (WritablePositionedInfo) getScriptInstance(context, resourceHandler, defer, encodeURL);
530 if (checkAlreadyAdded(info))
531 {
532 return;
533 }
534 HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
535 try
536 {
537 info.writePositionedInfo(response, context.getResponseWriter());
538 }
539 catch (IOException e)
540 {
541 throw new RuntimeException(e);
542 }
543 }
544
545 private boolean checkAlreadyAdded(PositionedInfo info)
546 {
547 Long key = new Long(info.hashCode());
548 if (alreadySeenResources.contains(key))
549 {
550 return true;
551 }
552
553 alreadySeenResources.add(key);
554 return false;
555 }
556
557
558
559
560
561 public void addStyleSheet(FacesContext context, ResourcePosition position,
562 Class myfacesCustomComponent, String resourceName)
563 {
564 addStyleSheet(context, position, new MyFacesResourceHandler(myfacesCustomComponent,
565 resourceName));
566 }
567
568
569
570
571
572 public void addStyleSheet(FacesContext context, ResourcePosition position, String uri)
573 {
574 uri = getAbsoluteUri(context, uri);
575
576 addStyleSheet(context, getStyleInstance(context, uri));
577 }
578
579 protected String getAbsoluteUri(FacesContext context, String uri)
580 {
581 if (uri.startsWith("/"))
582 {
583 return uri;
584 }
585
586 StringBuffer sb = new StringBuffer(80);
587 if (context.getExternalContext().getRequestPathInfo() != null)
588 {
589 sb.append(context.getExternalContext().getRequestPathInfo());
590 }
591 sb.append("/");
592 sb.append(uri);
593
594 return sb.toString();
595 }
596
597 private void addStyleSheet(FacesContext context, StreamablePositionedInfo styleInstance)
598 {
599 if (checkAlreadyAdded(styleInstance))
600 {
601 return;
602 }
603 StreamingThreadManager manager = (StreamingThreadManager) context.getExternalContext().getApplicationMap().get(StreamingThreadManager.KEY);
604 getHeaderInfoEntry().addInfo(styleInstance);
605 }
606
607
608
609
610
611 public void addStyleSheet(FacesContext context, ResourcePosition position,
612 ResourceHandler resourceHandler)
613 {
614 validateResourceHandler(resourceHandler);
615 addStyleSheet(context, getStyleInstance(context, resourceHandler));
616 }
617
618
619
620
621 public void addInlineStyleAtPosition(FacesContext context, ResourcePosition position, String inlineStyle)
622 {
623 addStyleSheet(context, getInlineStyleInstance(inlineStyle));
624 }
625
626
627
628
629 public void addInlineScriptAtPosition(FacesContext context, ResourcePosition position,
630 String inlineScript)
631 {
632 WritablePositionedInfo info = (WritablePositionedInfo) getInlineScriptInstance(inlineScript);
633 if (checkAlreadyAdded(info))
634 {
635 return;
636 }
637 HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
638 try
639 {
640 info.writePositionedInfo(response, context.getResponseWriter());
641 }
642 catch (IOException e)
643 {
644 throw new RuntimeException(e);
645 }
646 }
647
648 public String getResourceUri(FacesContext context, Class myfacesCustomComponent,
649 String resource, boolean withContextPath)
650 {
651 return getResourceUri(context,
652 new MyFacesResourceHandler(myfacesCustomComponent, resource), withContextPath);
653 }
654
655 public String getResourceUri(FacesContext context, Class myfacesCustomComponent, String resource)
656 {
657 return getResourceUri(context, new MyFacesResourceHandler(myfacesCustomComponent, resource));
658 }
659
660
661
662
663 public String getResourceUri(FacesContext context, ResourceHandler resourceHandler)
664 {
665 String uri = resourceHandler.getResourceUri(context);
666 if (uri == null)
667 {
668 return getResourceUri(context, resourceHandler.getResourceLoaderClass(), true);
669 }
670 return getResourceUri(context, resourceHandler.getResourceLoaderClass(), true) + uri;
671 }
672
673
674
675
676 public String getResourceUri(FacesContext context, ResourceHandler resourceHandler,
677 boolean withContextPath)
678 {
679 String uri = resourceHandler.getResourceUri(context);
680 if (uri == null)
681 {
682 return getResourceUri(context, resourceHandler.getResourceLoaderClass(),
683 withContextPath);
684 }
685 return getResourceUri(context, resourceHandler.getResourceLoaderClass(), withContextPath)
686 + uri;
687 }
688
689
690
691
692 public String getResourceUri(FacesContext context, String uri)
693 {
694 return getResourceUri(context, uri, true);
695 }
696
697
698
699
700 public String getResourceUri(FacesContext context, String uri, boolean withContextPath)
701 {
702 if (withContextPath)
703 {
704 return context.getApplication().getViewHandler().getResourceURL(context, uri);
705 }
706 return uri;
707 }
708
709
710
711
712 protected String getResourceUri(FacesContext context, Class resourceLoader,
713 boolean withContextPath)
714 {
715 StringBuffer sb = new StringBuffer(200);
716 sb.append(MyfacesConfig.getCurrentInstance(context.getExternalContext()).getResourceVirtualPath());
717 sb.append(PATH_SEPARATOR);
718 sb.append(resourceLoader.getName());
719 sb.append(PATH_SEPARATOR);
720 sb.append(getCacheKey(context));
721 sb.append(PATH_SEPARATOR);
722 return getResourceUri(context, sb.toString(), withContextPath);
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756 protected long getCacheKey(FacesContext context)
757 {
758
759 Map applicationMap = context.getExternalContext().getApplicationMap();
760 Long cacheKey = (Long) applicationMap.get(RESOURCES_CACHE_KEY);
761 if (cacheKey == null)
762 {
763 cacheKey = new Long(System.currentTimeMillis() / 100000);
764 applicationMap.put(RESOURCES_CACHE_KEY, cacheKey);
765 }
766 return cacheKey.longValue();
767 }
768
769 public boolean isResourceUri(ServletContext servletContext, HttpServletRequest request)
770 {
771 String path;
772 if (_contextPath != null)
773 {
774 path = _contextPath + getResourceVirtualPath(servletContext);
775 }
776 else
777 {
778 path = getResourceVirtualPath(servletContext);
779 }
780
781
782
783
784
785 try
786 {
787 if(request.getRequestURI().startsWith(URLEncoder.encode(path,"UTF-8")))
788 return true;
789 }
790 catch (UnsupportedEncodingException e)
791 {
792 log.error("Unsupported encoding UTF-8 used",e);
793
794 }
795
796 return request.getRequestURI().startsWith(path);
797 }
798
799 private Class getClass(String className) throws ClassNotFoundException
800 {
801 Class clazz = ClassUtils.classForName(className);
802 validateResourceLoader(clazz);
803 return clazz;
804 }
805
806 public void serveResource(ServletContext context, HttpServletRequest request,
807 HttpServletResponse response) throws IOException
808 {
809 String pathInfo = request.getPathInfo();
810 String uri = request.getContextPath() + request.getServletPath()
811 + (pathInfo == null ? "" : pathInfo);
812 String classNameStartsAfter = getResourceVirtualPath(context) + '/';
813
814 int posStartClassName = uri.indexOf(classNameStartsAfter) + classNameStartsAfter.length();
815 int posEndClassName = uri.indexOf(PATH_SEPARATOR, posStartClassName);
816 String className = uri.substring(posStartClassName, posEndClassName);
817 int posEndCacheKey = uri.indexOf(PATH_SEPARATOR, posEndClassName + 1);
818 String resourceUri = null;
819 if (posEndCacheKey + 1 < uri.length())
820 {
821 resourceUri = uri.substring(posEndCacheKey + 1);
822 }
823
824 try
825 {
826 Class resourceLoader = getClass(className);
827 validateResourceLoader(resourceLoader);
828 ((ResourceLoader) resourceLoader.newInstance()).serveResource(context, request,
829 response, resourceUri);
830
831
832
833
834
835
836
837
838
839
840 }
841 catch (ClassNotFoundException e)
842 {
843 log.error("Could not find class for name: " + className, e);
844 sendError(response, HttpServletResponse.SC_NOT_FOUND,
845 "Could not find resourceloader class for name: " + className);
846 }
847 catch (InstantiationException e)
848 {
849 log.error("Could not instantiate class for name: " + className, e);
850 sendError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
851 "Could not instantiate resourceloader class for name: " + className);
852 }
853 catch (IllegalAccessException e)
854 {
855 log.error("Could not access class for name: " + className, e);
856 sendError(response, HttpServletResponse.SC_FORBIDDEN,
857 "Could not access resourceloader class for name: " + className);
858 }
859 catch (IOException e)
860 {
861 logSend.error("Error while serving resource: " +resourceUri+", message : "+ e.getMessage(), e);
862 sendError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
863 }
864 catch (Throwable e)
865 {
866 log.error("Unknown error while serving resource: " +resourceUri+", message : "+ e.getMessage(), e);
867 sendError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
868 }
869 }
870
871 protected void sendError(HttpServletResponse response, int errorCode, String errorText)
872 throws IOException
873 {
874 try
875 {
876 response.sendError(errorCode, errorText);
877 }
878 catch (IllegalStateException e)
879 {
880 logSend.error("Could not send error, maybe some data has already been sent.", e);
881 }
882 }
883
884 public boolean hasHeaderBeginInfos(HttpServletRequest request)
885 {
886 throw new UnsupportedOperationException();
887 }
888
889
890
891
892 public void parseResponse(HttpServletRequest request, String bufferedResponse,
893 HttpServletResponse response)
894 {
895 throw new UnsupportedOperationException();
896 }
897
898
899
900
901 public void writeMyFacesJavascriptBeforeBodyEnd(HttpServletRequest request,
902 HttpServletResponse response) throws IOException
903 {
904 throw new UnsupportedOperationException();
905 }
906
907
908
909
910
911
912
913
914 public void writeWithFullHeader(HttpServletRequest request,
915 HttpServletResponse response) throws IOException
916 {
917 throw new UnsupportedOperationException();
918 }
919
920
921
922
923 public void writeResponse(HttpServletRequest request,
924 HttpServletResponse response) throws IOException
925 {
926 throw new UnsupportedOperationException();
927 }
928
929 private StylePositionedInfo getStyleInstance(FacesContext context, ResourceHandler resourceHandler)
930 {
931 return new StylePositionedInfo(getResourceUri(context, resourceHandler));
932 }
933
934 private PositionedInfo getScriptInstance(FacesContext context, ResourceHandler resourceHandler,
935 boolean defer, boolean encodeUrl)
936 {
937 return new ScriptPositionedInfo(getResourceUri(context, resourceHandler), defer, encodeUrl);
938 }
939
940 private StylePositionedInfo getStyleInstance(FacesContext context, String uri)
941 {
942 return new StylePositionedInfo(getResourceUri(context, uri));
943 }
944
945 protected PositionedInfo getScriptInstance(FacesContext context, String uri, boolean defer)
946 {
947 return new ScriptPositionedInfo(getResourceUri(context, uri), defer);
948 }
949
950 private PositionedInfo getInlineScriptInstance(String inlineScript)
951 {
952 return new InlineScriptPositionedInfo(inlineScript);
953 }
954
955 private InlineStylePositionedInfo getInlineStyleInstance(String inlineStyle)
956 {
957 return new InlineStylePositionedInfo(inlineStyle);
958 }
959
960 protected interface PositionedInfo
961 {
962 }
963
964 protected interface WritablePositionedInfo extends PositionedInfo
965 {
966 public abstract void writePositionedInfo(HttpServletResponse response, ResponseWriter writer)
967 throws IOException;
968 }
969
970 protected interface StreamablePositionedInfo extends PositionedInfo
971 {
972 public abstract void writePositionedInfo(HttpServletResponse response, PrintWriter writer)
973 throws IOException;
974 }
975
976 private abstract class AbstractResourceUri
977 {
978 protected final String _resourceUri;
979
980 protected AbstractResourceUri(String resourceUri)
981 {
982 _resourceUri = resourceUri;
983 }
984
985 public int hashCode()
986 {
987 return _resourceUri.hashCode();
988 }
989
990 public boolean equals(Object obj)
991 {
992 if (obj == null)
993 {
994 return false;
995 }
996 if (obj == this)
997 {
998 return true;
999 }
1000 if (obj instanceof AbstractResourceUri)
1001 {
1002 AbstractResourceUri other = (AbstractResourceUri) obj;
1003 return _resourceUri.equals(other._resourceUri);
1004 }
1005 return false;
1006 }
1007
1008 protected String getResourceUri()
1009 {
1010 return _resourceUri;
1011 }
1012 }
1013
1014 private class StylePositionedInfo extends AbstractResourceUri implements WritablePositionedInfo, StreamablePositionedInfo
1015 {
1016 protected StylePositionedInfo(String resourceUri)
1017 {
1018 super(resourceUri);
1019 }
1020
1021 public void writePositionedInfo(HttpServletResponse response, ResponseWriter writer)
1022 throws IOException
1023 {
1024 writer.startElement(HTML.LINK_ELEM, null);
1025 writer.writeAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.REL_ATTR, org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.STYLESHEET_VALUE, null);
1026 writer.writeAttribute(HTML.HREF_ATTR, response.encodeURL(this.getResourceUri()), null);
1027 writer.writeAttribute(HTML.TYPE_ATTR, HTML.STYLE_TYPE_TEXT_CSS, null);
1028 writer.endElement(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.LINK_ELEM);
1029 }
1030
1031 public void writePositionedInfo(HttpServletResponse response, PrintWriter writer) throws IOException
1032 {
1033 writer.println();
1034 writer.write("@import url(\"");
1035 writer.write(response.encodeURL(this.getResourceUri()));
1036 writer.write("\");");
1037 writer.println();
1038 }
1039 }
1040
1041 private class ScriptPositionedInfo extends AbstractResourceUri implements
1042 WritablePositionedInfo
1043 {
1044 protected final boolean _defer;
1045 protected final boolean _encodeUrl;
1046
1047 public ScriptPositionedInfo(String resourceUri, boolean defer)
1048 {
1049 this(resourceUri, defer, true);
1050 }
1051
1052 public ScriptPositionedInfo(String resourceUri, boolean defer, boolean encodeUrl)
1053 {
1054 super(resourceUri);
1055 _defer = defer;
1056 _encodeUrl = encodeUrl;
1057 }
1058
1059 public int hashCode()
1060 {
1061 return new HashCodeBuilder()
1062 .append(this.getResourceUri())
1063 .append(_defer)
1064 .append(_encodeUrl)
1065 .toHashCode();
1066 }
1067
1068 public boolean equals(Object obj)
1069 {
1070 if (super.equals(obj))
1071 {
1072 if (obj instanceof ScriptPositionedInfo)
1073 {
1074 ScriptPositionedInfo other = (ScriptPositionedInfo) obj;
1075 return new EqualsBuilder()
1076 .append(_defer, other._defer)
1077 .append(_encodeUrl, other._encodeUrl)
1078 .isEquals();
1079 }
1080 }
1081 return false;
1082 }
1083
1084 public void writePositionedInfo(HttpServletResponse response, ResponseWriter writer)
1085 throws IOException
1086 {
1087 writer.startElement(HTML.SCRIPT_ELEM, null);
1088 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
1089 if (_encodeUrl)
1090 {
1091 writer.writeAttribute(HTML.SRC_ATTR, response.encodeURL(this.getResourceUri()), null);
1092 }
1093 else
1094 {
1095 writer.writeAttribute(HTML.SRC_ATTR, this.getResourceUri(), null);
1096 }
1097
1098 if (_defer)
1099 {
1100 writer.writeAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_ELEM_DEFER_ATTR, "true", null);
1101 }
1102 writer.endElement(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_ELEM);
1103 }
1104 }
1105
1106 private abstract class InlinePositionedInfo implements WritablePositionedInfo
1107 {
1108 private final String _inlineValue;
1109
1110 protected InlinePositionedInfo(String inlineValue)
1111 {
1112 _inlineValue = inlineValue;
1113 }
1114
1115 public String getInlineValue()
1116 {
1117 return _inlineValue;
1118 }
1119
1120 public int hashCode()
1121 {
1122 return new HashCodeBuilder().append(_inlineValue).toHashCode();
1123 }
1124
1125 public boolean equals(Object obj)
1126 {
1127 if (obj == null)
1128 {
1129 return false;
1130 }
1131 if (obj == this)
1132 {
1133 return true;
1134 }
1135 if (obj instanceof InlinePositionedInfo)
1136 {
1137 InlinePositionedInfo other = (InlinePositionedInfo) obj;
1138 return new EqualsBuilder().append(_inlineValue, other._inlineValue).isEquals();
1139 }
1140 return false;
1141 }
1142 }
1143
1144 private class InlineScriptPositionedInfo extends InlinePositionedInfo
1145 {
1146 protected InlineScriptPositionedInfo(String inlineScript)
1147 {
1148 super(inlineScript);
1149 }
1150
1151 public void writePositionedInfo(HttpServletResponse response, ResponseWriter writer)
1152 throws IOException
1153 {
1154 writer.startElement(HTML.SCRIPT_ELEM, null);
1155 writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
1156 writer.writeText(getInlineValue(), null);
1157 writer.endElement(HTML.SCRIPT_ELEM);
1158 }
1159 }
1160
1161 private class InlineStylePositionedInfo extends InlinePositionedInfo implements StreamablePositionedInfo
1162 {
1163 protected InlineStylePositionedInfo(String inlineStyle)
1164 {
1165 super(inlineStyle);
1166 }
1167
1168 public void writePositionedInfo(HttpServletResponse response, ResponseWriter writer)
1169 throws IOException
1170 {
1171 writer.startElement(HTML.STYLE_ELEM, null);
1172 writer.writeAttribute(HTML.REL_ATTR, HTML.STYLESHEET_VALUE, null);
1173 writer.writeAttribute(HTML.TYPE_ATTR, HTML.STYLE_TYPE_TEXT_CSS, null);
1174 writer.writeText(getInlineValue(), null);
1175 writer.endElement(HTML.STYLE_ELEM);
1176 }
1177
1178 public void writePositionedInfo(HttpServletResponse response, PrintWriter writer) throws IOException
1179 {
1180 writer.println();
1181 writer.write(getInlineValue());
1182 writer.println();
1183 }
1184 }
1185
1186 public boolean requiresBuffer()
1187 {
1188 return false;
1189 }
1190
1191 protected StreamingThreadManager.HeaderInfoEntry getHeaderInfoEntry()
1192 {
1193 if (headerInfoEntry == null)
1194 {
1195 throw new IllegalStateException("responseStarted() needs to be called first");
1196 }
1197
1198 return headerInfoEntry;
1199 }
1200
1201 public void responseStarted()
1202 {
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214 }
1215
1216 public void responseFinished()
1217 {
1218 getHeaderInfoEntry().setRequestDone();
1219 }
1220
1221 public void responseStarted(Object context, Object request)
1222 {
1223 if(ExternalContextUtils.getRequestType(context, request).isPortlet())
1224 {
1225 StreamingThreadManager manager = (StreamingThreadManager) PortletUtils.getAttribute(context, StreamingThreadManager.KEY);
1226 requestId = manager.putNewHeaderInfoEntry();
1227 headerInfoEntry = manager.getHeaderInfo(requestId);
1228 }
1229 else
1230 {
1231 StreamingThreadManager manager = (StreamingThreadManager) ((ServletContext)context).getAttribute(StreamingThreadManager.KEY);
1232 requestId = manager.putNewHeaderInfoEntry();
1233 headerInfoEntry = manager.getHeaderInfo(requestId);
1234 }
1235 }
1236
1237 public boolean hasHeaderBeginInfos()
1238 {
1239 return false;
1240 }
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281 public void addStyleLoaderHere(FacesContext context, Class myfacesCustomComponent) throws IOException
1282 {
1283 ResponseWriter writer = context.getResponseWriter();
1284
1285 writer.startElement(HTML.LINK_ELEM, null);
1286 writer.writeAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.REL_ATTR, org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.STYLESHEET_VALUE, null);
1287 writer.writeAttribute(HTML.HREF_ATTR,
1288 getResourceUri(context,
1289 new StreamingResourceHandler(requestId + "/header.css"),
1290 true), null);
1291 writer.writeAttribute(HTML.TYPE_ATTR, HTML.STYLE_TYPE_TEXT_CSS, null);
1292 writer.endElement(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.LINK_ELEM);
1293 }
1294
1295 private String getResourceVirtualPath(ServletContext servletContext)
1296 {
1297 if(resourceVirtualPath == null)
1298 {
1299 resourceVirtualPath = servletContext.getInitParameter(MyfacesConfig.INIT_PARAM_RESOURCE_VIRTUAL_PATH);
1300
1301 if(resourceVirtualPath == null)
1302 {
1303 resourceVirtualPath = MyfacesConfig.INIT_PARAM_RESOURCE_VIRTUAL_PATH_DEFAULT;
1304 }
1305 }
1306
1307 return resourceVirtualPath;
1308 }
1309 }