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  
20  package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.commons.fileupload.FileItem;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIFile;
24  import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
25  import org.apache.myfaces.tobago.internal.webapp.TobagoMultipartFormdataRequest;
26  import org.apache.myfaces.tobago.layout.Measure;
27  import org.apache.myfaces.tobago.renderkit.InputRendererBase;
28  import org.apache.myfaces.tobago.renderkit.css.Classes;
29  import org.apache.myfaces.tobago.renderkit.css.Style;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
33  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
34  import org.apache.myfaces.tobago.util.ComponentUtils;
35  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import javax.faces.component.UIComponent;
40  import javax.faces.context.FacesContext;
41  import javax.servlet.ServletRequest;
42  import javax.servlet.http.HttpServletRequestWrapper;
43  import java.io.IOException;
44  
45  public class FileRenderer extends InputRendererBase {
46  
47    private static final Logger LOG = LoggerFactory.getLogger(FileRenderer.class);
48  
49    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
50      super.prepareRender(facesContext, component);
51      FacesContextUtils.setEnctype(facesContext, "multipart/form-data");
52    }
53  
54    public boolean getRendersChildren() {
55      return true;
56    }
57  
58    public void decode(FacesContext facesContext, UIComponent component) {
59      if (ComponentUtils.isOutputOnly(component)) {
60        return;
61      }
62  
63      AbstractUIFile input = (AbstractUIFile) component;
64  
65      TobagoMultipartFormdataRequest request = null;
66      Object requestObject = facesContext.getExternalContext().getRequest();
67      if (requestObject instanceof TobagoMultipartFormdataRequest) {
68        request = (TobagoMultipartFormdataRequest) requestObject;
69      } else if (requestObject instanceof HttpServletRequestWrapper) {
70        ServletRequest wrappedRequest
71            = ((HttpServletRequestWrapper) requestObject).getRequest();
72        if (wrappedRequest instanceof TobagoMultipartFormdataRequest) {
73          request = (TobagoMultipartFormdataRequest) wrappedRequest;
74        }
75      }
76      // TODO PortletRequest ??
77      if (request == null) {
78        // should not be possible, because of the check in UIPage
79        LOG.error("Can't process multipart/form-data without TobagoRequest. "
80            + "Please check the web.xml and define a TobagoMultipartFormdataFilter. "
81            + "See documentation for <tc:file>");
82      } else {
83  
84        FileItem item = request.getFileItem(input.getClientId(facesContext));
85  
86        if (LOG.isDebugEnabled()) {
87          LOG.debug("Uploaded file name : \"" + item.getName()
88              + "\"  size = " + item.getSize());
89        }
90        input.setSubmittedValue(item);
91        //TODO remove this
92        input.setValid(true);
93      }
94    }
95  
96    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
97  
98      final AbstractUIFile file = (AbstractUIFile) component;
99      final String clientId = file.getClientId(facesContext);
100     final Style style = new Style(facesContext, file);
101 
102     final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
103 
104     writer.startElement(HtmlElements.DIV, file);
105     writer.writeIdAttribute(clientId);
106     writer.writeClassAttribute(Classes.create(file));
107     HtmlRendererUtils.writeDataAttributes(facesContext, writer, file);
108     writer.writeStyleAttribute(style);
109 
110     // visible fake input for a pretty look
111     final Style inputStyle = new Style();
112     final Measure prettyWidthSub = getResourceManager().getThemeMeasure(facesContext, file, "prettyWidthSub");
113     inputStyle.setWidth(style.getWidth().subtract(prettyWidthSub));
114     writer.startElement(HtmlElements.INPUT, file);
115     writer.writeIdAttribute(clientId + ComponentUtils.SUB_SEPARATOR + "pretty");
116     writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.TEXT, false);
117     writer.writeClassAttribute(Classes.create(file, "pretty"));
118     writer.writeStyleAttribute(inputStyle);
119     writer.writeAttribute(HtmlAttributes.DISABLED, true);
120     // TODO Focus
121     //HtmlRendererUtils.renderFocus(clientId, file.isFocus(), ComponentUtils.isError(file), facesContext, writer);
122     writer.endElement(HtmlElements.INPUT);
123 
124     // invisible file input
125     writer.startElement(HtmlElements.INPUT, file);
126     writer.writeIdAttribute(clientId + ComponentUtils.SUB_SEPARATOR + "real");
127     writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.FILE, false);
128     writer.writeClassAttribute(Classes.create(file, "real"));
129     writer.writeNameAttribute(clientId);
130     // readonly seems not making sense in browsers.
131     writer.writeAttribute(HtmlAttributes.DISABLED, file.isDisabled() || file.isReadonly());
132     writer.writeAttribute(HtmlAttributes.READONLY, file.isReadonly());
133     writer.writeAttribute(HtmlAttributes.REQUIRED, file.isRequired());
134     writer.writeAttribute(HtmlAttributes.SIZE, "1024", false);
135     final Integer tabIndex = file.getTabIndex();
136     if (tabIndex != null) {
137       writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
138     }
139     final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, file);
140     if (title != null) {
141       writer.writeAttribute(HtmlAttributes.TITLE, title, true);
142     }
143     writer.endElement(HtmlElements.INPUT);
144 
145     writer.endElement(HtmlElements.DIV);
146   }
147 }