resource.java

来自「It is a java server faces tab component.」· Java 代码 · 共 142 行

JAVA
142
字号
package com.cim.jsf.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.faces.context.FacesContext;
import java.util.Map;


public class Resource
		extends HttpServlet {
	private static final String COMPONENTS_PACKAGE =
			"com.cim.jsf.component.";
	public static final String RESOURCE_VIRTUAL_PATH = "/pjsf.resource";

	private static final Logger logger = Logger.getLogger(
			"com.cim.jsf.servlet.Resource Logger" );

	//Initialize global variables
	public void init() throws ServletException {
	}

	//Process the HTTP Get request
	public void doGet( HttpServletRequest request, HttpServletResponse response ) throws
			ServletException, IOException {
		String uri = request.getRequestURI();
		int idxResourcePath = uri.indexOf( RESOURCE_VIRTUAL_PATH );
		int idxComponent = idxResourcePath + RESOURCE_VIRTUAL_PATH.length() + 1;
		int idxResource = uri.indexOf( "/", idxComponent ) + 1;
		String componentName = uri.substring( idxComponent, idxResource - 1 );
		String resourceFileName = uri.substring( idxResource );
		// Set Content Type
		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.
		}
		// Get Class
		Class component = null;
		try {
			component = Class.forName( COMPONENTS_PACKAGE + componentName );
		} catch ( ClassNotFoundException e ) {
			logger.log( Level.SEVERE, "Class(" + COMPONENTS_PACKAGE + componentName +
						") doesn't exist" );
			return;
		}
		// Get Resource File and write to response
		InputStream rcIs = component.getResourceAsStream( "resource/" + resourceFileName );
		if ( rcIs == null ) {
			throw new IOException( "Unable to find resource " + resourceFileName +
								   " for component " + componentName + "." );
		}
		OutputStream resOs = response.getOutputStream();
		int c = -1;
		while ( ( c = rcIs.read() ) != -1 ) {
			resOs.write( c );
		}
		resOs.close();
		rcIs.close();
	}

	//Process the HTTP Post request
	public void doPost( HttpServletRequest request,
						HttpServletResponse response ) throws
			ServletException, IOException {
		doGet( request, response );
	}

	//Clean up resources
	public void destroy() {
	}

	// ----
	public static String getHeader( FacesContext context, Class componentClass,
									String resourceFileName ) {
		HttpServletRequest request = ( HttpServletRequest )context.
				getExternalContext().getRequest();
		String contextPath = request.getContextPath();
		//
		Map map = context.getExternalContext().getRequestMap();
		String resourceKey = componentClass.getName() + "_" + resourceFileName;
		Boolean isEncoded = ( Boolean )map.get( resourceKey );
		if ( isEncoded == null || !isEncoded.booleanValue() ) {
			String compnentName = getComponentName( componentClass );
			if ( resourceFileName.endsWith( ".js" ) ) {
				return MessageFormat.format(
						"<script type=\"text/javascript\" src=\"{0}{1}/{2}/{3}\"></script>",
						new Object[] {contextPath, RESOURCE_VIRTUAL_PATH,
						compnentName, resourceFileName} );
			} else if ( resourceFileName.endsWith( ".css" ) ) {
				return MessageFormat.format(
						"<link type=\"text/css\" rel=\"stylesheet\" href=\"{0}{1}/{2}/{3}\"></link>",
						new Object[] {contextPath, RESOURCE_VIRTUAL_PATH,
						compnentName, resourceFileName} );
			}
			// set map to true
			map.put( resourceKey, Boolean.TRUE );
		}
		return null;
	}

	public static String getResourceString( FacesContext context, Class componentClass,
									String resourceFileName ) {
		HttpServletRequest request = ( HttpServletRequest )context.
				getExternalContext().getRequest();
		String contextPath = request.getContextPath();
		String compnentName = getComponentName( componentClass );
		return MessageFormat.format( "{0}{1}/{2}/{3}", new Object[] {contextPath,
									 RESOURCE_VIRTUAL_PATH, compnentName,
									 resourceFileName} );
	}

	public static String getComponentName( Class componentClass ) {
		String name = componentClass.getName();
		if ( !name.startsWith( COMPONENTS_PACKAGE ) ) {
			logger.log( Level.SEVERE,
						"The class( " + name + ") is not a PJSF component." );
			return null;
		}
		name = name.substring( COMPONENTS_PACKAGE.length() );
		return name;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?