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.example.demo;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.enterprise.context.SessionScoped;
26  import javax.faces.application.FacesMessage;
27  import javax.faces.context.FacesContext;
28  import javax.faces.event.AjaxBehaviorEvent;
29  import javax.inject.Named;
30  import javax.servlet.http.Part;
31  import java.io.Serializable;
32  import java.lang.invoke.MethodHandles;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  @SessionScoped
37  @Named
38  public class UploadController implements Serializable {
39  
40    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
41  
42    private Part fileBasic;
43    private Part fileContentType;
44    private Part[] fileMulti;
45    private Part fileAjax;
46    private List<UploadItem> uploadItems = new ArrayList<>();
47  
48    public String uploadBasic() {
49      upload(fileBasic);
50      return null;
51    }
52  
53    public String uploadContentType() {
54      upload(fileContentType);
55      return null;
56    }
57  
58    public String uploadMulti() {
59      for (final Part part : fileMulti) {
60        upload(part);
61      }
62      return null;
63    }
64  
65    public void uploadAjax(final AjaxBehaviorEvent event) {
66      upload(fileAjax);
67    }
68  
69    private void upload(final Part part) {
70      LOG.info("checking file item");
71      if (part == null || part.getSize() == 0) {
72        return;
73      }
74      LOG.info("type=" + part.getContentType());
75      LOG.info("size=" + part.getSize());
76      LOG.info("cd = " + part.getHeader("Content-Disposition"));
77      final String submittedFileName = part.getSubmittedFileName();
78      LOG.info("name=" + submittedFileName);
79      uploadItems.add(new UploadItem(submittedFileName, part.getSize(), part.getContentType()));
80      FacesContext.getCurrentInstance().addMessage(
81              null, new FacesMessage(FacesMessage.SEVERITY_INFO, "File was uploaded: " + submittedFileName, null));
82    }
83  
84    public Part getFileBasic() {
85      return fileBasic;
86    }
87  
88    public void setFileBasic(final Part fileBasic) {
89      this.fileBasic = fileBasic;
90    }
91  
92    public Part getFileContentType() {
93      return fileContentType;
94    }
95  
96    public void setFileContentType(final Part fileContentType) {
97      this.fileContentType = fileContentType;
98    }
99  
100   public Part[] getFileMulti() {
101     return fileMulti;
102   }
103 
104   public void setFileMulti(final Part[] fileMulti) {
105     this.fileMulti = fileMulti;
106   }
107 
108   public Part getFileAjax() {
109     return fileAjax;
110   }
111 
112   public void setFileAjax(final Part fileAjax) {
113     this.fileAjax = fileAjax;
114   }
115 
116   public List<UploadItem> getUploadItems() {
117     return uploadItems;
118   }
119 }