📄 addresource.java
字号:
// Skip cache key int posStartResourceFileName = uri.indexOf("/", posEndComponentName+1)+1; String resourceFileName = uri.substring(posStartResourceFileName); return new String[]{componentName, resourceFileName}; } private static String getComponentName(Class componentClass){ String name = componentClass.getName(); if( ! name.startsWith(COMPONENTS_PACKAGE) ){ log.error("getComponentName called for non extension component : "+name+"\n"+ "For security reasons, only components member of the "+COMPONENTS_PACKAGE+" are allowed to add ressources."); return null; } name = name.substring( COMPONENTS_PACKAGE.length() ); return name; } static Class getComponent(String componentName) throws ClassNotFoundException{ return Class.forName( COMPONENTS_PACKAGE+componentName ); } static private InputStream getResource(String componentName, String resourceFileName) { Class component; try { component = getComponent(componentName); } catch (ClassNotFoundException e) { log.error("Class not found for component "+componentName); return null; } while( resourceFileName.startsWith(".") || resourceFileName.startsWith("/") || resourceFileName.startsWith("\\") ) resourceFileName = resourceFileName.substring(1); return component.getResourceAsStream( "resource/"+resourceFileName ); } static public void serveResource(HttpServletRequest request, HttpServletResponse response) throws IOException{ String[] resourceInfo = getResourceInfoFromPath(request); String componentName = resourceInfo[0]; String resourceFileName = resourceInfo[1]; log.debug("Serving resource "+resourceFileName+" for component "+componentName); String lcResourceFileName = resourceFileName.toLowerCase(); if( lcResourceFileName.endsWith(".js") ) response.setContentType("text/javascript"); else if( lcResourceFileName.endsWith(".css") ) response.setContentType("text/css"); else if( lcResourceFileName.endsWith(".gif") ) response.setContentType("image/gif"); else if( lcResourceFileName.endsWith(".png") ) response.setContentType("image/png"); else if( lcResourceFileName.endsWith(".jpg") || lcResourceFileName.endsWith(".jpeg") ) response.setContentType("image/jpeg"); else if( lcResourceFileName.endsWith(".xml") || lcResourceFileName.endsWith(".xsl") ) response.setContentType("text/xml"); // XSL has to be served as XML. InputStream is = getResource(componentName, resourceFileName); if( is == null ){ throw new IOException("Unable to find resource "+resourceFileName+" for component "+componentName+ ". Check that this file is available in the classpath in sub-directory /resource of the component-directory."); } response.setDateHeader("Last-Modified", getLastModified()); // Set browser cache to a week. // There is no risk, as the cache key is part of the URL. Calendar expires = Calendar.getInstance(); expires.add(Calendar.DAY_OF_YEAR, 7); response.setDateHeader("Expires", expires.getTimeInMillis()); OutputStream os = response.getOutputStream(); int c; while ((c = is.read()) != -1) os.write(c); os.close(); } // Header stuffs private static Set getAdditionalHeaderInfoToRender(HttpServletRequest request){ Set set = (Set) request.getAttribute(ADDITIONAL_HEADER_INFO_REQUEST_ATTRUBITE_NAME); if( set == null ){ set = new LinkedHashSet(); request.setAttribute(ADDITIONAL_HEADER_INFO_REQUEST_ATTRUBITE_NAME, set); } return set; } private static void addAdditionalHeaderInfoToRender(FacesContext context, AdditionalHeaderInfoToRender info){ HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest(); Set set = getAdditionalHeaderInfoToRender( request ); set.add( info ); } static public boolean hasAdditionalHeaderInfoToRender(HttpServletRequest request){ return request.getAttribute(ADDITIONAL_HEADER_INFO_REQUEST_ATTRUBITE_NAME) != null; } static public void writeWithFullHeader(HttpServletRequest request, ExtensionsResponseWrapper responseWrapper, HttpServletResponse response) throws IOException{ String originalResponse = responseWrapper.toString(); // try the most common cases first boolean addHeaderTags = false; int insertPosition = originalResponse.indexOf( "</head>" ); if( insertPosition < 0 ){ insertPosition = originalResponse.indexOf( "</HEAD>" ); if( insertPosition < 0 ){ insertPosition = originalResponse.indexOf( "<body" ); addHeaderTags = true; if( insertPosition < 0 ){ insertPosition = originalResponse.indexOf( "<BODY" ); addHeaderTags = true; if( insertPosition < 0 ){ // the two most common cases head/HEAD and body/BODY did not work, so we try it with lowercase String lowerCase = originalResponse.toLowerCase(response.getLocale()); insertPosition = lowerCase.indexOf( "</head>" ); if( insertPosition < 0 ){ insertPosition = lowerCase.indexOf( "<body" ); addHeaderTags = true; } } } } if( insertPosition < 0 ){ log.warn("Response has no <head> or <body> tag:\n"+originalResponse); insertPosition = 0; } } PrintWriter writer = response.getWriter(); if( insertPosition > 0 ) writer.write( originalResponse.substring(0, insertPosition) ); if( addHeaderTags ) writer.write("<head>"); for(Iterator i = getAdditionalHeaderInfoToRender(request).iterator(); i.hasNext() ;){ AdditionalHeaderInfoToRender headerInfo = (AdditionalHeaderInfoToRender) i.next(); writer.write( headerInfo.getString(request) ); } if( addHeaderTags ) writer.write("</head>"); writer.write( originalResponse.substring(insertPosition) ); } private static class AdditionalHeaderInfoToRender{ static final int TYPE_JS = 0; static final int TYPE_CSS = 1; static final int TYPE_CSS_INLINE = 2; public int type; public boolean deferJS = false; public String componentName; public String resourceFileName; public String inlineText; public AdditionalHeaderInfoToRender(int infoType, Class componentClass, String resourceFileName) { this.type = infoType; this.componentName = getComponentName(componentClass); this.resourceFileName = resourceFileName; } public AdditionalHeaderInfoToRender(int infoType, Class componentClass, String resourceFileName, boolean defer) { if( defer && infoType != TYPE_JS ) log.error("Defer can only be used for scripts."); this.type = infoType; this.componentName = getComponentName(componentClass); this.resourceFileName = resourceFileName; this.deferJS = defer; } public AdditionalHeaderInfoToRender(int infoType, String inlineText) { if( infoType != TYPE_CSS_INLINE ) log.error("This constructor only supports TYPE_CSS_INLINE"); this.type = infoType; this.inlineText = inlineText; } public int hashCode() { return (componentName+((char)7) +resourceFileName+((char)7) +(type+""+((char)7)) +(inlineText+""+((char)7)) +(deferJS+"")).hashCode(); } public boolean equals(Object obj) { if( !(obj instanceof AdditionalHeaderInfoToRender) ) return false; AdditionalHeaderInfoToRender toCompare = (AdditionalHeaderInfoToRender) obj; if( type != toCompare.type || deferJS != toCompare.deferJS ) return false; if( componentName == null ){ if( toCompare.componentName != null ) return false; }else if( ! componentName.equals(toCompare.componentName) ) return false; if( resourceFileName == null ){ if( toCompare.resourceFileName != null ) return false; }else if( ! resourceFileName.equals(toCompare.resourceFileName) ) return false; if( inlineText == null ) return toCompare.inlineText == null; return inlineText.equals(toCompare.inlineText); } public String getString(HttpServletRequest request){ switch (type) { case TYPE_JS: return "<script " +"src=\""+getResourceMappedPath(componentName, resourceFileName, request)+"\" " +(deferJS ? "defer=\"true\" " : "") +"type=\"text/javascript\"" +">" +"</script>\n"; case TYPE_CSS: return "<link rel=\"stylesheet\" " +"href=\""+getResourceMappedPath(componentName, resourceFileName, request)+"\" " +"type=\"text/css\"/>\n"; case TYPE_CSS_INLINE: return "<style type=\"text/css\">"+inlineText+"</style>\n"; default: log.warn("Unknown type:"+type); return "<link href=\""+"\"/>\n"; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -