View Javadoc

1   package org.musicontroller.streaming;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.io.OutputStream;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import org.apache.log4j.Logger;
12  import org.apache.tapestry.IRequestCycle;
13  import org.apache.tapestry.engine.IEngineService;
14  import org.apache.tapestry.engine.ILink;
15  import org.apache.tapestry.services.LinkFactory;
16  import org.apache.tapestry.services.ServiceConstants;
17  import org.apache.tapestry.util.ContentType;
18  import org.apache.tapestry.web.WebResponse;
19  import org.musicontroller.service.McService;
20  import org.varienaja.util.FileOperations;
21  
22  public class CoverArtService implements IEngineService {
23  	private static final Logger log = Logger.getLogger(CoverArtService.class);
24  
25  	public static final String SERVICE_NAME = "coverart";
26  
27      private LinkFactory _linkFactory;
28      private WebResponse _response;
29      
30      /**
31       * Link to the MusiController service. This will be provided by HiveMind.
32       */
33  	private McService _mcService;
34  	
35  	/**
36  	 * Getter for the MusiController Service.
37  	 * @return The MusicController Service.
38  	 */
39  	public McService getMcService() {
40  		return _mcService;
41  	}
42  	
43  	/**
44  	 * Setter for the MusiController service.
45  	 * @param service The MusiController service.
46  	 */
47  	public void setMcService(McService service) {
48  		_mcService = service;
49  	}
50  	
51  	public ILink getLink(boolean post, Object parameter) {
52  		Object[] params = (Object[]) parameter;
53  		String playlistid = params[0]==null ? "0" : Long.toString((Long) params[0]);
54  		String size = params[1]==null ? "200" : Integer.toString((Integer) params[1]);
55  		
56          Map<String,String> parameters = new HashMap<String,String>();
57          parameters.put(ServiceConstants.SERVICE, getName());
58          parameters.put("playlistid",playlistid);
59          parameters.put("size",size);
60          
61          return _linkFactory.constructLink(this, false, parameters, true);
62  	}
63  	
64  	public void service(IRequestCycle cycle) throws IOException {
65  		long playlistid = Long.parseLong(cycle.getParameter("playlistid"));
66  		int size = Integer.parseInt(cycle.getParameter("size"));
67  		
68  		log.debug("Coverart ("+size+"x"+size+") asked for playlistid: "+playlistid);
69  
70  		File coverArtFile = getMcService().attemptToDownloadCoverArt(playlistid,size);
71  		//TODO If still nothing found, output the 'no-image-available'-image.
72  		
73  		OutputStream out = _response.getOutputStream(new ContentType("image/jpeg"));
74  		_response.setHeader("Cache-Control", "max-age=181440000, must-revalidate"); //Cache one year.
75  		InputStream in = null;
76  		try {
77  			in = new FileInputStream(coverArtFile);
78  			FileOperations.copyStream(in,out);
79  		} catch (Exception e) {
80  			log.error(e);
81  		} finally {
82  			if(out!=null) {
83  				out.close();
84  			}
85  			if(in!=null) {
86  				in.close();
87  			}
88  		}
89  	}
90  	
91  	public String getName() {
92  		return SERVICE_NAME;
93  	}
94  
95      public void setLinkFactory(LinkFactory linkFactory) {
96          _linkFactory = linkFactory;
97      }
98  
99      public void setResponse(WebResponse response) {
100         _response = response;
101     }	
102 }