View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.service.image;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPathService;
38  import fr.paris.lutece.portal.service.util.AppPropertiesService;
39  
40  import java.io.File;
41  import java.io.FileInputStream;
42  import java.io.IOException;
43  import java.io.OutputStream;
44  
45  import javax.servlet.ServletContext;
46  import javax.servlet.ServletException;
47  import javax.servlet.http.HttpServlet;
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  
52  /**
53   * Servlet serving document file resources
54   */
55  public class ImageServlet extends HttpServlet
56  {
57      private static final long serialVersionUID = -5713203328367191908L;
58      private static final String PARAMETER_RESOURCE_TYPE = "resource_type";
59      private static final String PARAMETER_ID = "id";
60      private static final String PROPERTY_PATH_IMAGES = "path.images.root";
61      private static final String PROPERTY_IMAGE_PAGE_DEFAULT = "image.page.default";
62  
63      /**
64       * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
65       * methods.
66       * @param request servlet request
67       * @param response servlet response
68       * @throws ServletException the servlet Exception
69       * @throws IOException the io exception
70       */
71      protected void processRequest( HttpServletRequest request, HttpServletResponse response )
72          throws ServletException, IOException
73      {
74          String strResourceId = request.getParameter( PARAMETER_ID );
75          String strResourceTypeId = request.getParameter( PARAMETER_RESOURCE_TYPE );
76  
77          ImageResource image;
78  
79          if ( strResourceId != null )
80          {
81              int nResourceId = Integer.parseInt( strResourceId );
82              image = ImageResourceManager.getImageResource( strResourceTypeId, nResourceId );
83  
84              // Test the field image value
85              if ( getImageExist( image ) )
86              {
87                  response.setContentType( image.getMimeType(  ) );
88  
89                  OutputStream out = response.getOutputStream(  );
90                  out.write( image.getImage(  ) );
91                  out.flush(  );
92                  out.close(  );
93              }
94              else
95              {
96                  ServletContext sc = getServletContext(  );
97                  String strImageUrl = AppPathService.getAbsolutePathFromRelativePath( AppPropertiesService.getProperty( 
98                              PROPERTY_PATH_IMAGES ) + "/" +
99                          AppPropertiesService.getProperty( PROPERTY_IMAGE_PAGE_DEFAULT ) ); //
100                 response.setContentType( sc.getMimeType( strImageUrl ) );
101 
102                 File file = new File( strImageUrl );
103                 response.setContentLength( (int) file.length(  ) );
104 
105                 FileInputStream in = null;
106 
107                 try
108                 {
109                     //Open the file and output streams
110                     in = new FileInputStream( file );
111 
112                     OutputStream out = response.getOutputStream(  );
113 
114                     // Copy the contents of the file to the output stream
115                     byte[] buf = new byte[1024];
116                     int count = 0;
117 
118                     while ( ( count = in.read( buf ) ) >= 0 )
119                     {
120                         out.write( buf, 0, count );
121                     }
122 
123                     in.close(  );
124                     out.close(  );
125                 }
126                 catch ( IOException e )
127                 {
128                     AppLogService.error( e.getMessage(  ), e );
129                     throw e;
130                 }
131                 finally
132                 {
133                     if ( in != null )
134                     {
135                         in.close(  );
136                     }
137                 }
138             }
139         }
140     }
141 
142     /**
143      * Handles the HTTP <code>GET</code> method.
144      * @param request servlet request
145      * @param response servlet response
146      * @throws ServletException the servlet Exception
147      * @throws IOException the io exception
148      */
149     @Override
150     protected void doGet( HttpServletRequest request, HttpServletResponse response )
151         throws ServletException, IOException
152     {
153         processRequest( request, response );
154     }
155 
156     /**
157      * Handles the HTTP <code>POST</code> method.
158      * @param request servlet request
159      * @param response servlet response
160      * @throws ServletException the servlet Exception
161      * @throws IOException the io exception
162      */
163     @Override
164     protected void doPost( HttpServletRequest request, HttpServletResponse response )
165         throws ServletException, IOException
166     {
167         processRequest( request, response );
168     }
169 
170     /**
171      * Returns a short description of the servlet.
172      * @return message
173      */
174     @Override
175     public String getServletInfo(  )
176     {
177         return "Servlet serving images content";
178     }
179 
180     /**
181      * Test the existence of an image in the base.
182      * If the size of the contents of the field is null or lower or
183      * equal 1, nImageLength is with false, otherwise it's true
184      * @param image The Resource Image
185      * @return true if images exist, otherwise return false
186      */
187     private boolean getImageExist( ImageResource image )
188     {
189         if ( image.getImage(  ) != null )
190         {
191             int nImageLength = image.getImage(  ).length;
192 
193             if ( nImageLength >= 1 )
194             {
195                 return true;
196             }
197         }
198 
199         return false;
200     }
201 }