View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.captcha;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.URL;
25  import java.util.Collections;
26  import java.util.Map;
27  
28  import javax.faces.application.Resource;
29  import javax.faces.application.ResourceHandler;
30  import javax.faces.context.FacesContext;
31  
32  import org.apache.myfaces.shared_tomahawk.resource.ResourceHandlerSupport;
33  
34  
35  public class CAPTCHAResource extends Resource
36  {
37  
38      private String _captchaSessionKeyName;
39      private ResourceHandlerSupport _resourceHandlerSupport;
40      
41      public CAPTCHAResource(ResourceHandlerSupport support, String captchaSessionKeyName, String resourceName)
42      {
43          super();
44          _resourceHandlerSupport = support;
45          _captchaSessionKeyName = captchaSessionKeyName;
46          setLibraryName(CAPTCHAResourceHandlerWrapper.CAPTCHA_LIBRARY);
47          setResourceName(resourceName);
48          setContentType("image/png");
49      }
50  
51      @Override
52      public InputStream getInputStream() throws IOException
53      {
54          // Return an InputStream forces buffer the image, and really it is not necessary.
55          // so, we just use CAPTCHAResourceHandlerWrapper to intercept requests and render
56          // the image there directly.
57          return null;
58      }
59      
60      static class WrappedByteArrayOutputStream extends ByteArrayOutputStream{
61          
62          public WrappedByteArrayOutputStream(){
63              super();
64          }
65          
66          public WrappedByteArrayOutputStream(int size){
67              super(size);                
68          }
69          
70          private byte[] getInnerArray(){
71              return buf; 
72          }
73          
74          private int getInnerCount(){
75              return count;
76          }
77      }
78  
79      @Override
80      public String getRequestPath()
81      {
82          String path;
83          FacesContext facesContext = FacesContext.getCurrentInstance();
84  
85          if (_resourceHandlerSupport.isExtensionMapping())
86          {
87              path = ResourceHandler.RESOURCE_IDENTIFIER + '/' + 
88                  getResourceName() + _resourceHandlerSupport.getMapping();
89          }
90          else
91          {
92              String mapping = _resourceHandlerSupport.getMapping(); 
93              path = ResourceHandler.RESOURCE_IDENTIFIER + '/' + getResourceName();
94              path = (mapping == null) ? path : mapping + path;
95          }
96          path = path + "?ln=" + getLibraryName() + "&captchaSessionKeyName=" + _captchaSessionKeyName + "&dummyParameter=" + System.currentTimeMillis();
97  
98          return facesContext.getApplication().getViewHandler().getResourceURL(facesContext, path);
99      }
100 
101     @Override
102     public Map<String, String> getResponseHeaders()
103     {
104         return Collections.emptyMap();
105     }
106 
107     @Override
108     public URL getURL()
109     {
110         // This is a generated resource, so it does not have an inner url, return null
111         return null;
112     }
113 
114     @Override
115     public boolean userAgentNeedsUpdate(FacesContext context)
116     {
117         return true;
118     }
119 }