1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
55
56
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
111 return null;
112 }
113
114 @Override
115 public boolean userAgentNeedsUpdate(FacesContext context)
116 {
117 return true;
118 }
119 }