1 6 14 package com.sun.javaee.blueprints.components.ui.example; 15 16 import java.io.DataInputStream ; 17 import java.io.DataOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.net.URL ; 22 import java.net.URLConnection ; 23 import javax.faces.event.PhaseEvent; 24 import javax.faces.event.PhaseId; 25 import javax.faces.event.PhaseListener; 26 import javax.faces.context.FacesContext; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.el.MethodExpression; 29 30 37 public class CompBPhaseListener implements PhaseListener { 38 39 public static final String PATH_PREFIX = "/META-INF"; 40 public static final String SCRIPT_SUFFIX = ".js"; 41 public static final String CSS_SUFFIX = ".css"; 42 public static final String GIF_SUFFIX = ".gif"; 43 public static final String JPG_SUFFIX = ".jpg"; 44 public static final String PNG_SUFFIX = ".png"; 45 public static final String APP_KEY = "/jsf-example/"; 46 public static final String ACTION_KEY = "/bpui_compb_action/"; 47 public static final boolean bDebug=false; 48 49 public CompBPhaseListener() { 50 } 51 52 55 public void afterPhase(PhaseEvent event) { 56 FacesContext context=event.getFacesContext(); 57 String rootId=context.getViewRoot().getViewId(); 58 59 if(bDebug) System.out.println("PhaseListener - Root ID " + rootId); 60 int iPos=rootId.indexOf(APP_KEY); 61 int iPosx=rootId.indexOf(ACTION_KEY); 62 63 if (rootId.endsWith(SCRIPT_SUFFIX) && iPos > -1) { 65 handleResourceRequest(event, PATH_PREFIX + rootId, "text/javascript"); 66 } else if (rootId.endsWith(CSS_SUFFIX) && iPos > -1) { 67 handleResourceRequest(event, PATH_PREFIX + rootId, "text/css"); 68 } else if (rootId.endsWith(GIF_SUFFIX) && iPos > -1) { 69 handleResourceRequest(event, PATH_PREFIX + rootId, "image/gif"); 70 } else if (rootId.endsWith(JPG_SUFFIX) && iPos > -1) { 71 handleResourceRequest(event, PATH_PREFIX + rootId, "image/jpeg"); 72 } else if (rootId.endsWith(PNG_SUFFIX) && iPos > -1) { 73 handleResourceRequest(event, PATH_PREFIX + rootId, "image/x-png"); 74 } else if (iPosx > -1) { 75 String methodx="#{" + rootId.substring(iPos + ACTION_KEY.length() + 1) + "}"; 77 methodx=methodx.replace('/','.'); 78 if(bDebug) System.out.println("Method expression to call = " + methodx); 79 80 try { 81 Class [] argTypes = { PhaseEvent.class}; 82 MethodExpression mex=context.getApplication().getExpressionFactory().createMethodExpression(context.getELContext(), 83 methodx, null, argTypes); 84 Object [] args = { event }; 85 mex.invoke(context.getELContext(), args); 86 } catch (Exception e) { 87 e.printStackTrace(); 89 } 90 91 92 } 93 } 94 95 96 100 private void handleResourceRequest(PhaseEvent event, String resource, String contentType) { 101 102 URL sxURL = CompBPhaseListener.class.getResource(resource); 104 if(bDebug) System.out.println("Resource url = " + sxURL); 105 if(sxURL == null) return; 107 108 HttpServletResponse response=(HttpServletResponse )event.getFacesContext().getExternalContext().getResponse(); 109 OutputStream outStream=null; 110 try { 111 response.setContentType(contentType); 112 response.setStatus(200); 113 outStream=response.getOutputStream(); 114 readWriteBinaryUtil(sxURL, outStream); 116 } catch (Exception e) { 117 String message = "Can't find resource \"" + resource + "\" in the same directory as the class - " + getClass().getResource("PopupPhaseListener.class"); 118 System.out.println(message); 119 e.printStackTrace(); 120 } finally { 121 try { 122 outStream.flush(); 123 } catch (Exception ee) {} 125 event.getFacesContext().responseComplete(); 126 } 127 } 128 129 130 131 public void beforePhase(PhaseEvent event) { 132 } 133 134 public PhaseId getPhaseId() { 135 return PhaseId.RESTORE_VIEW; 136 } 137 138 142 public static void readWriteBinaryUtil(URL sxURL, OutputStream outStream) throws IOException { 143 144 DataOutputStream outData=null; 145 DataInputStream inData=null; 146 int byteCnt=0; 147 byte[] buffer=new byte[4096]; 148 149 if(bDebug) System.out.println("RW Loading - " + sxURL); 151 try { 152 outData=new DataOutputStream (outStream); 153 inData=new DataInputStream (openStream(sxURL)); 154 155 while ((byteCnt=inData.read(buffer)) != -1) { 156 if (outData != null && byteCnt > 0) { 157 outData.write(buffer, 0, byteCnt); 158 } 159 } 160 } catch (IOException e) { 161 throw e; 162 } finally { 163 try { 164 if(inData != null) { 165 inData.close(); 166 } 167 } catch (IOException ioe) {} 168 } 169 } 170 171 172 private static InputStream openStream(URL sxURL) throws IOException { 173 InputStream isx=null; 175 if(sxURL!= null) { 176 URLConnection urlConn = sxURL.openConnection(); 177 urlConn.setUseCaches(false); 178 isx=urlConn.getInputStream(); 179 } 180 return isx; 181 } 182 183 } 184 | Popular Tags |