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.bestpractice;
21  
22  import org.apache.commons.io.IOUtils;
23  import org.apache.myfaces.tobago.example.demo.DemoException;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import javax.enterprise.context.RequestScoped;
28  import javax.faces.context.FacesContext;
29  import javax.inject.Named;
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.lang.invoke.MethodHandles;
34  
35  @RequestScoped
36  @Named
37  public class BestPracticeController {
38  
39    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
40  
41    private String status;
42  
43    public String throwException() {
44      throw new DemoException("This exception is forced by the user.");
45    }
46  
47    public String viewPdfInBrowser() {
48      return viewFile(false, true);
49    }
50  
51    public String viewPdfOutsideOfBrowser() {
52      return viewFile(true, true);
53    }
54  
55    public String viewFile(final boolean outside, final boolean pdf) {
56  
57      final FacesContext facesContext = FacesContext.getCurrentInstance();
58  
59      try (final InputStream inputStream = getInputStream(pdf, facesContext)) {
60        final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
61        response.setContentType(pdf ? "application/pdf" : "text/plain");
62        if (outside) {
63          response.setHeader("Content-Disposition", "attachment; filename=x-sample." + (pdf ? "pdf" : "txt"));
64        }
65        IOUtils.copy(inputStream, response.getOutputStream());
66      } catch (final IOException e) {
67        LOG.warn("Cannot deliver " + (pdf ? "pdf" : "txt"), e);
68        return "error"; // response via faces
69      }
70      facesContext.responseComplete();
71      return null;
72    }
73  
74    private InputStream getInputStream(boolean pdf, FacesContext facesContext) {
75      final String path = "content/30-concept/24-non-faces-response/x-sample." + (pdf ? "pdf" : "txt");
76      InputStream inputStream = facesContext.getExternalContext().getResourceAsStream(path);
77      if (inputStream == null) {
78        inputStream = facesContext.getExternalContext().getResourceAsStream("/" + path);
79      }
80      return inputStream;
81    }
82  
83    public String getStatus() {
84      return status;
85    }
86  
87    public void setStatus(final String status) {
88      this.status = status;
89    }
90  }