View Javadoc

1   /*
2    * $Id: ImageContentAccessorHandler.java,v 1.16 2010/03/19 14:53:48 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.images;
5   
6   import javax.faces.FacesException;
7   import javax.faces.context.FacesContext;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  import org.rcfaces.core.image.GeneratedImageInformation;
12  import org.rcfaces.core.image.IGeneratedImageInformation;
13  import org.rcfaces.core.image.IImageOperation;
14  import org.rcfaces.core.internal.RcfacesContext;
15  import org.rcfaces.core.internal.contentAccessor.BasicContentAccessor;
16  import org.rcfaces.core.internal.contentAccessor.BasicGenerationResourceInformation;
17  import org.rcfaces.core.internal.contentAccessor.ContentAccessorFactory;
18  import org.rcfaces.core.internal.contentAccessor.ContentAccessorsRegistryImpl;
19  import org.rcfaces.core.internal.contentAccessor.FiltredContentAccessor;
20  import org.rcfaces.core.internal.contentAccessor.IContentAccessor;
21  import org.rcfaces.core.internal.contentAccessor.IContentAccessorHandler;
22  import org.rcfaces.core.internal.contentAccessor.IContentPath;
23  import org.rcfaces.core.internal.contentAccessor.IGeneratedResourceInformation;
24  import org.rcfaces.core.internal.contentAccessor.IGenerationResourceInformation;
25  import org.rcfaces.core.lang.IContentFamily;
26  import org.rcfaces.core.provider.AbstractProvider;
27  
28  /**
29   * 
30   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
31   * @version $Revision: 1.16 $ $Date: 2010/03/19 14:53:48 $
32   */
33  public abstract class ImageContentAccessorHandler extends AbstractProvider
34          implements IContentAccessorHandler {
35      private static final String REVISION = "$Revision: 1.16 $";
36  
37      private static final Log LOG = LogFactory
38              .getLog(ImageContentAccessorHandler.class);
39  
40      public static final String IMAGE_CONTENT_PROVIDER_ID = "org.rcfaces.core.IMAGE_CONTENT_PROVIDER";
41  
42      public abstract IImageOperation getImageOperation(String operationId);
43  
44      public abstract IImageResourceAdapter[] listImageResourceAdapters(
45              String contentType, String suffix);
46  
47      public ImageContentAccessorHandler() {
48      }
49  
50      public void startup(FacesContext facesContext) {
51          super.startup(facesContext);
52  
53          RcfacesContext rcfacesContext = RcfacesContext
54                  .getInstance(facesContext);
55  
56          ((ContentAccessorsRegistryImpl) rcfacesContext
57                  .getContentAccessorRegistry()).declareContentAccessorHandler(
58                  IContentFamily.IMAGE, this);
59      }
60  
61      protected abstract IContentAccessor formatImageURL(
62              FacesContext facesContext, IContentAccessor contentAccessor,
63              IGeneratedImageInformation generatedImageInformation,
64              IGenerationResourceInformation generationInformation);
65  
66      public IContentAccessor handleContent(FacesContext facesContext,
67              IContentAccessor contentAccessor,
68              IGeneratedResourceInformation[] contentInformationRef,
69              IGenerationResourceInformation generationInformation) {
70  
71          // if (contentAccessor.getPathType() !=
72          // IContentAccessor.FILTER_PATH_TYPE) {
73          // return null;
74          // }
75  
76          Object content = contentAccessor.getContentRef();
77          if ((content instanceof String) == false) {
78              return null;
79          }
80  
81          if (isProviderEnabled() == false) {
82              if (LOG.isDebugEnabled()) {
83                  LOG
84                          .debug("Provider is disabled, return an unsupported content accessor flag");
85              }
86  
87              if (contentAccessor.getPathType() == IContentPath.FILTER_PATH_TYPE) {
88                  return ContentAccessorFactory.UNSUPPORTED_CONTENT_ACCESSOR;
89              }
90  
91              return null;
92          }
93  
94          String url = (String) content;
95  
96          GeneratedImageInformation imageGeneratedInformation = null;
97          IGeneratedResourceInformation generatedInformation = contentInformationRef[0];
98          if (generatedInformation instanceof GeneratedImageInformation) {
99              imageGeneratedInformation = (GeneratedImageInformation) generatedInformation;
100 
101         } else {
102             imageGeneratedInformation = new GeneratedImageInformation();
103 
104             contentInformationRef[0] = imageGeneratedInformation;
105         }
106 
107         if (generationInformation == null) {
108             generationInformation = new BasicGenerationResourceInformation();
109         }
110         generationInformation.setAttribute(
111                 IGenerationResourceInformation.SOURCE_KEY, url);
112 
113         IContentAccessor modifiedContentAccessor = contentAccessor;
114 
115         if (contentAccessor.getPathType() == IContentPath.FILTER_PATH_TYPE) {
116 
117             int idx = url.indexOf(IContentAccessor.FILTER_SEPARATOR);
118             String filter = url.substring(0, idx);
119 
120             if (idx == url.length() - 2) { // Filtre tout seul !
121                 IContentAccessor parentAccessor = contentAccessor
122                         .getParentAccessor();
123 
124                 if (parentAccessor == null) {
125                     throw new FacesException("Can not get main image of '"
126                             + url + "'.");
127                 }
128 
129                 modifiedContentAccessor = new FiltredContentAccessor(filter,
130                         parentAccessor);
131 
132             } else {
133                 String newURL = url.substring(idx
134                         + IContentAccessor.FILTER_SEPARATOR.length());
135 
136                 modifiedContentAccessor = new FiltredContentAccessor(filter,
137                         new BasicContentAccessor(facesContext, newURL,
138                                 contentAccessor,
139                                 IContentPath.UNDEFINED_PATH_TYPE));
140             }
141         }
142 
143         IContentAccessor formattedContentAccessor = formatImageURL(
144                 facesContext, modifiedContentAccessor,
145                 imageGeneratedInformation, generationInformation);
146 
147         if (LOG.isDebugEnabled()) {
148             LOG.debug("formattedContentAccessor=" + formattedContentAccessor);
149         }
150 
151         return formattedContentAccessor;
152     }
153 
154     public abstract boolean isProviderEnabled();
155 
156     public abstract String getMimeType(String url);
157 
158     public abstract int getValidContenType(String contentType);
159 
160     public static boolean isOperationSupported(FacesContext facesContext,
161             String operationId, IContentAccessor imageContentAccessor) {
162         RcfacesContext rcfacesContext = RcfacesContext
163                 .getInstance(facesContext);
164 
165         ImageContentAccessorHandler imageOperationRepository = (ImageContentAccessorHandler) rcfacesContext
166                 .getProvidersRegistry().getProvider(
167                         ImageContentAccessorHandler.IMAGE_CONTENT_PROVIDER_ID);
168 
169         if (imageOperationRepository == null) {
170             return false;
171         }
172 
173         return imageOperationRepository.isOperationSupported(operationId,
174                 imageContentAccessor);
175     }
176 
177     protected abstract boolean isOperationSupported(String operationId,
178             IContentAccessor imageContentAccessor);
179 }