View Javadoc
1   /*
2    * Copyright (c) 2002-2018, 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.plugins.stock.modules.billetterie.web;
35  
36  import fr.paris.lutece.plugins.stock.modules.tickets.service.IShowService;
37  import fr.paris.lutece.portal.service.spring.SpringContextService;
38  
39  import org.apache.commons.io.IOUtils;
40  import org.apache.commons.lang.StringUtils;
41  
42  import org.apache.log4j.Logger;
43  
44  import java.io.IOException;
45  
46  import javax.servlet.ServletException;
47  import javax.servlet.ServletOutputStream;
48  import javax.servlet.http.HttpServlet;
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  /**
53   * Used for special solr queries
54   *
55   * @author abataille
56   */
57  public class PosterImageServlet extends HttpServlet
58  {
59      private static final String ERROR_MESSAGE = "Appel de PosterImageServlet avec un id de produit null";
60      private static final String CONTENT_TYPE_IMAGE_JPEG = "image/jpeg";
61      private static final String PARAMETER_TB = "tb";
62      private static final String PARAMETER_REAL_IMAGE = "img_real";
63      private static final String PARAMETER_PRODUCT_ID = "product_id";
64      private static final String BEAN_STOCK_TICKETS_SHOW_SERVICE = "stock-tickets.showService";
65  
66      /**
67       *
68       */
69      private static final long serialVersionUID = -1165966480942742905L;
70      private static final Logger LOGGER = Logger.getLogger( PosterImageServlet.class );
71  
72      /** The product service. */
73      private IShowService _productService = (IShowService) SpringContextService.getBean( BEAN_STOCK_TICKETS_SHOW_SERVICE );
74  
75      /**
76       * Returns poster image
77       *
78       * @param request
79       *            the request
80       * @param response
81       *            the response
82       * @throws ServletException
83       *             the servlet exception
84       * @throws IOException
85       *             Signals that an I/O exception has occurred.
86       */
87      protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
88      {
89          String sIdProduct = request.getParameter( PARAMETER_PRODUCT_ID );
90  
91          if ( StringUtils.isNotEmpty( sIdProduct ) )
92          {
93              Integer idProduct = Integer.parseInt( sIdProduct );
94              boolean isThumbnail = ( request.getParameter( PARAMETER_TB ) != null ) && request.getParameter( PARAMETER_TB ).equals( String.valueOf( true ) );
95              boolean isImageReal = ( request.getParameter( PARAMETER_REAL_IMAGE ) != null )
96                      && request.getParameter( PARAMETER_REAL_IMAGE ).equals( String.valueOf( true ) );
97              byte [ ] bImage;
98  
99              if ( isImageReal )
100             {
101                 bImage = _productService.getRealImage( idProduct );
102             }
103             else
104             {
105                 if ( isThumbnail )
106                 {
107                     bImage = _productService.getTbImage( idProduct );
108                 }
109                 else
110                 {
111                     bImage = _productService.getImage( idProduct );
112                 }
113             }
114             response.setContentLength( bImage.length );
115             response.setContentType( CONTENT_TYPE_IMAGE_JPEG );
116 
117             ServletOutputStream os = response.getOutputStream( );
118             IOUtils.write( bImage, os );
119             os.flush( );
120             os.close( );
121         }
122         else
123         {
124             LOGGER.error( ERROR_MESSAGE );
125         }
126     }
127 }