001 package org.apache.myfaces.tobago.example.demo.bestpractice;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 import org.apache.commons.io.IOUtils;
021 import org.apache.commons.logging.Log;
022 import org.apache.commons.logging.LogFactory;
023
024 import javax.faces.context.FacesContext;
025 import javax.servlet.http.HttpServletResponse;
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.util.Arrays;
029 import java.util.List;
030
031
032 public class BestPracticeController {
033
034 private static final Log LOG = LogFactory.getLog(BestPracticeController.class);
035
036 private List<Bird> birds = Arrays.asList(
037 new Bird("Amsel", 25),
038 new Bird("Drossel", 25),
039 new Bird("Fink", 9),
040 new Bird("Star", 19)
041 );
042
043 private String status;
044
045 public String throwException() {
046 throw new RuntimeException("This exception is forced by the user.");
047 }
048
049 public String viewPdfInBrowser() {
050 return viewPdf(false);
051 }
052
053 public String viewPdfOutsideOfBrowser() {
054 return viewPdf(true);
055 }
056
057 private String viewPdf(boolean outside) {
058
059 FacesContext facesContext = FacesContext.getCurrentInstance();
060
061 InputStream inputStream = null;
062 try {
063 inputStream = facesContext.getExternalContext().getResourceAsStream("best-practice/sample.pdf");
064 if (inputStream == null) {
065 inputStream = facesContext.getExternalContext().getResourceAsStream("/best-practice/sample.pdf");
066 }
067 HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
068 response.setContentType("application/pdf");
069 if (outside) {
070 response.setHeader("Content-Disposition", "attachment; filename=sample.pdf");
071 }
072 IOUtils.copy(inputStream, response.getOutputStream());
073 } catch (IOException e) {
074 LOG.warn("Cannot deliver pdf", e);
075 return "error"; // response via faces
076 } finally {
077 IOUtils.closeQuietly(inputStream);
078 }
079 facesContext.responseComplete();
080 return null;
081 }
082
083 public List<Bird> getBirds() {
084 return birds;
085 }
086
087 public String getStatus() {
088 return status;
089 }
090
091 public void setStatus(String status) {
092 this.status = status;
093 }
094 }