View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstart.xml.upload;
18  
19  import java.io.InputStream;
20  import java.io.Serializable;
21  import java.util.List;
22  
23  import javax.enterprise.context.SessionScoped;
24  import javax.enterprise.inject.Produces;
25  import javax.inject.Inject;
26  import javax.inject.Named;
27  
28  import org.jboss.as.quickstart.xml.Book;
29  import org.jboss.as.quickstart.xml.Errors;
30  import org.jboss.as.quickstart.xml.XMLParser;
31  
32  /**
33   * Backing bean to provide example with required logic to parse provided XML file.<br>
34   * 
35   * @author baranowb
36   * 
37   */
38  /*
39   * Annotated as: - SessionScope bean to tie its lifecycle to session. This is required to make it shared between UploadServlet
40   * invocation and JSF actions.
41   */
42  @SessionScoped
43  public class FileUploadBean implements Serializable {
44  
45      /**
46       * 
47       */
48      private static final long serialVersionUID = -4542914921835861304L;
49  
50      // data, catalog which is displayed in h:dataTable
51      private List<Book> catalog;
52  
53      @Inject
54      private Errors errors;
55  
56      /*
57       * Inject XMLParsor with 'Catalog' as type. Instance is created by container. Implementation alternative is controlled in
58       * beans.xml
59       */
60      @Inject
61      private XMLParser xmlParser;
62  
63      /**
64       * Getter for books catalog.
65       */
66      @Produces
67      @Named
68      public List<Book> getCatalog() {
69          return catalog;
70      }
71  
72      /**
73       * Action method invoked from UploadServlet once it parses request with 'multipart/form-data' form data and fetches uploaded
74       * file.
75       */
76      public void parseUpload(InputStream is) {
77          try {
78              // Trigger parser and clear errors
79              this.errors.getErrorMessages().clear();
80              this.catalog = xmlParser.parse(is);
81          } catch (Exception e) {
82              this.errors.addErrorMessage("warning", e);
83              return;
84          }
85      }
86  }