View Javadoc
1   /******************************************************************************
2    * BVLRestServiceImpl.java - Implement the client facing rest service
3    * $Id: BVLRestServiceImpl.java,v 1.23 2015/05/11 01:57:54 dick Exp $
4    * 
5    * BuckoVidLib - The BuckoSoft Video Library
6    * Copyright(c) 2014 - Dick Balaska
7    * 
8    * $Log: BVLRestServiceImpl.java,v $
9    * Revision 1.23  2015/05/11 01:57:54  dick
10   * /statistics wants a session so we don't count restricted movies.
11   *
12   * Revision 1.22  2015/05/06 17:11:10  dick
13   * Commentality.
14   *
15   * Revision 1.21  2015/05/01 12:43:48  dick
16   * Handy call /idToKey converts an id to a key for debugging.
17   *
18   * Revision 1.20  2015/04/30 06:34:27  dick
19   * Getting lists of VideoBase is always protected by session.
20   *
21   * Revision 1.19  2015/04/30 03:18:04  dick
22   * Deal mostly with VideoBase. Only fetch full Video when we really need it.
23   * Protect all (most) calls to the restricted section of the library by session.
24   *
25   * Revision 1.18  2015/04/27 15:27:44  dick
26   * Move service calls that want session protection to RestService.
27   *
28   * Revision 1.17  2015/04/02 23:51:33  dick
29   * actorVideos returns a list of Videos that an actor was in.
30   * recentVideos returns a list of recently added Videos.
31   *
32   * Revision 1.16  2015/04/01 02:46:30  dick
33   * Return a list of Videos for a Director.
34   *
35   * Revision 1.15  2015/03/29 04:06:37  dick
36   * Improve statistics.
37   *
38   * Revision 1.14  2015/03/27 19:47:44  dick
39   * Read the about and mia data from flat files in the config directory.
40   *
41   * Revision 1.13  2015/03/25 06:48:14  dick
42   * Return a DetailedVideo from a key.
43   *
44   * Revision 1.12  2015/03/21 09:20:20  dick
45   * sectionVideos(key) return the videos from one section.
46   * Sort the LibrarySections by number of entries in each section.
47   *
48   * Revision 1.11  2014/11/01 08:05:06  dick
49   * Count TV episodes.
50   *
51   * Revision 1.10  2014/10/31 06:55:11  dick
52   * Add /statistics
53   *
54   * Revision 1.9  2014/10/26 05:36:57  dick
55   * recentlyAdded status can display 2 videos.
56   *
57   * Revision 1.8  2014/10/25 22:52:39  dick
58   * Send the whole video in status, not just the key.  Client wants the title too.
59   *
60   * Revision 1.7  2014/10/23 05:00:10  dick
61   * WishList handling.
62   *
63   * Revision 1.6  2014/10/20 01:26:20  dick
64   * /status returns the libraryStatus.
65   *
66   * Revision 1.5  2014/10/17 08:12:10  dick
67   * Add the database and getFailedToRip().
68   *
69   * Revision 1.4  2014/10/08 04:37:09  dick
70   * Declare and implement fetch a random Video.
71   *
72   * Revision 1.3  2014/10/07 02:48:57  dick
73   * Implement getting the sectionList from plex.
74   *
75   * Revision 1.2  2014/10/06 18:30:57  dick
76   * Add a second fake library to aid debugging arrays. (Arrays of 1 are tough...)
77   *
78   * Revision 1.1  2014/10/06 08:10:21  dick
79   * Client facing rest service
80   *
81   */
82  package com.buckosoft.BuckoVidLib.web;
83  
84  import java.io.IOException;
85  import java.net.URISyntaxException;
86  import java.nio.charset.Charset;
87  import java.nio.file.Files;
88  import java.nio.file.Paths;
89  import java.util.List;
90  
91  import javax.jws.WebService;
92  import javax.ws.rs.GET;
93  import javax.ws.rs.NotFoundException;
94  import javax.ws.rs.Path;
95  import javax.ws.rs.PathParam;
96  import javax.ws.rs.Produces;
97  
98  import org.springframework.beans.factory.annotation.Autowired;
99  import org.springframework.stereotype.Service;
100 
101 import com.buckosoft.BuckoVidLib.business.BuckoVidLib;
102 import com.buckosoft.BuckoVidLib.db.Database;
103 import com.buckosoft.BuckoVidLib.domain.FailedToRip;
104 import com.buckosoft.BuckoVidLib.domain.VideoBase;
105 import com.buckosoft.BuckoVidLib.domain.WishList;
106 import com.buckosoft.BuckoVidLib.util.ConfigManager;
107 import com.buckosoft.BuckoVidLib.util.DomainConverter;
108 
109 
110 /** Implement the primary client facing rest service
111  * @author dick
112  * @since 2014-10-06
113  */
114 @WebService(endpointInterface = "com.buckosoft.BuckoVidLib.web.BVLRestService")
115 @Service("bvlService")
116 @Path("/")
117 @Produces("text/xml")
118 public class BVLRestServiceImpl implements BVLRestService {
119 
120 	@Autowired
121 	private BuckoVidLib		bvl;
122 
123 	@Autowired
124 	private	Database		db;
125 	
126 
127 	@Override
128 	@GET
129 	@Path("/failedToRip")
130 	public List<FailedToRip>	failedToRip() {
131 		return(bvl.getFailedToRip());
132 	}
133 	
134 	/* (non-Javadoc)
135 	 * @see com.buckosoft.BuckoVidLib.web.BVLRestService#wishList()
136 	 */
137 	@Override
138 	@GET
139 	@Path("/wishList")
140 	public List<WishList> wishList() {
141 		return(bvl.getWishList());
142 	}
143 
144 	/* (non-Javadoc)
145 	 * @see com.buckosoft.BuckoVidLib.web.BVLRestService#statistics()
146 	 */
147 
148 	/* (non-Javadoc)
149 	 * @see com.buckosoft.BuckoVidLib.web.BVLRestService#about()
150 	 */
151 	@Override
152 	@GET
153 	@Path("/about")
154 	@Produces("text/plain")
155 	public String about() {
156 		return(readAllLines("/BuckoVidLib-about.html"));
157 	}
158 
159 	
160 	/* (non-Javadoc)
161 	 * @see com.buckosoft.BuckoVidLib.web.BVLRestService#missingInAction()
162 	 */
163 	@Override
164 	@GET
165 	@Path("/statistics/mia")
166 	@Produces("text/plain")
167 	public String missingInAction() {
168 		return(readAllLines("/BuckoVidLib-MIA.html"));
169 	}
170 
171 	private	String readAllLines(String fileName) {
172 		List<String> ss;
173 		try {
174 			ss = Files.readAllLines(
175 				    Paths.get(this.getClass().getResource(fileName).toURI()), Charset.defaultCharset());
176 		} catch (IOException e) {
177 			return("<h2>failed to read about data</h2>");
178 		} catch (URISyntaxException e) {
179 			return("<h2>failed to determine about data</h2>");
180 		}
181 		String s = "";
182 		for (String sx : ss)
183 			s += sx + "\n";
184 		return s;
185 		
186 	}
187 
188 	@GET
189 	@Path("/idToKey/{id}")
190 	@Produces("text/plain")
191 	public String idToKey(@PathParam("id") Integer id) {
192 		if (!ConfigManager.getBoolean("BuckoVidLib.debugJS", false))
193 			return("Not in production");
194 		VideoBase v = db.getVideoBase(id);
195 		if (v == null) {
196 			return("Video " + id + " not found");
197 		}
198 		return(DomainConverter.intToKey(v.getHashKey()) + "\n");
199 	}
200 
201 	@GET
202 	@Path("/.*")
203 	public void unhandled() {
204 		throw new NotFoundException();
205 	}
206 
207 }