View Javadoc
1   /******************************************************************************
2    * AdminService.java - Handle REST admin service calls.
3    * $Id: AdminService.java,v 1.3 2015/05/03 17:54:22 dick Exp $
4    * 
5    * BuckoVidLib - The BuckoSoft Video Library
6    * Copyright(c) 2015 - Dick Balaska
7    * 
8    * $Log: AdminService.java,v $
9    * Revision 1.3  2015/05/03 17:54:22  dick
10   * Add a little logging.
11   *
12   * Revision 1.2  2015/05/01 12:43:14  dick
13   * Replace stub update runner with a real one.
14   *
15   * Revision 1.1  2015/04/30 14:50:34  dick
16   * Handle REST admin service calls.
17   *
18   */
19  package com.buckosoft.BuckoVidLib.web;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.JAXBException;
28  import javax.xml.bind.Marshaller;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.http.MediaType;
34  import org.springframework.stereotype.Controller;
35  import org.springframework.web.bind.annotation.RequestMapping;
36  
37  import com.buckosoft.BSAccount.BSAccountMan;
38  import com.buckosoft.BuckoVidLib.business.BuckoVidLib;
39  import com.buckosoft.BuckoVidLib.domain.rest.admin.RestRefreshStatus;
40  import com.buckosoft.BuckoVidLib.util.ConfigManager;
41  
42  /** Handle REST admin service calls.
43   * @author dick
44   * @since 2015-04-30
45   *
46   */
47  @Controller
48  @RequestMapping("/admin")
49  public class AdminService {
50  	private final Log log = LogFactory.getLog(getClass());
51  
52  	private	boolean logExceptions = true;
53  	private	boolean prettyPrint = true;
54  
55  	@Autowired
56  	BSAccountMan	bsAccountMan;
57  	
58  	@Autowired
59  	private BuckoVidLib		bvl;
60  
61  	public AdminService() {
62  		prettyPrint = ConfigManager.getBoolean("BuckoVidLib.prettyPrint", true);
63  		logExceptions = ConfigManager.getBoolean("BuckoVidLib.logExceptions", true);
64  	}
65  
66  	@RequestMapping(value="/refreshFromPlex", produces=MediaType.APPLICATION_XML_VALUE)
67  	public void refreshFromPlex(HttpServletRequest request, HttpServletResponse response){
68  		log.info("refreshFromPlex");
69  		if (!bvl.isAdmin(request)) {
70  			response.setStatus(401);
71  			log.warn("401 not authorized");
72  			return;
73  		}
74  		bvl.startRefreshLibraryFromPlex();
75  		RestRefreshStatus rrs = bvl.getRefreshLibraryFromPlexStatus();
76  		
77  		outputXml(response, rrs);
78  	}
79  	
80  	@RequestMapping(value="/refreshFromPlex/status", produces=MediaType.APPLICATION_XML_VALUE)
81  	public void refreshFromPlexStatus(HttpServletRequest request, HttpServletResponse response){
82  		if (!bvl.isAdmin(request)) {
83  			response.setStatus(401);
84  			log.warn("401 not authorized refreshFromPlexStatus");
85  			return;
86  		}
87  		RestRefreshStatus rrs = bvl.getRefreshLibraryFromPlexStatus();
88  		log.info("refreshFromPlexStatus " + rrs.getPercentComplete() + "%");
89  		outputXml(response, rrs);		
90  	}
91  
92  	private	void outputXml(HttpServletResponse response, Object data) {
93  		try {
94  			JAXBContext context;
95  			context = JAXBContext.newInstance(data.getClass());
96  			Marshaller m = context.createMarshaller();
97  			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, prettyPrint);
98  			OutputStream os;
99  			os = response.getOutputStream();
100 			m.marshal(data, os);
101 		} catch (IOException | JAXBException e) {
102 			if (logExceptions)
103 				e.printStackTrace();
104 			else
105 				log.warn(e.getMessage());
106 		}
107 
108 	}
109 }