1   package org.daisy.pipeline.tts.impl;
2   
3   import java.util.Collection;
4   
5   import org.daisy.common.properties.Properties;
6   import org.daisy.pipeline.tts.Voice;
7   import org.daisy.pipeline.tts.VoiceInfo.LanguageRange;
8   import org.daisy.pipeline.tts.VoiceManager;
9   import org.daisy.pipeline.webservice.restlet.AuthenticatedResource;
10  import org.daisy.pipeline.webservice.xml.XmlUtils;
11  
12  import org.restlet.data.MediaType;
13  import org.restlet.data.Status;
14  import org.restlet.ext.xml.DomRepresentation;
15  import org.restlet.representation.Representation;
16  import org.restlet.resource.Get;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import org.w3c.dom.Document;
22  import org.w3c.dom.Element;
23  
24  public class VoiceResource extends AuthenticatedResource {
25  
26  	private static final Logger logger = LoggerFactory.getLogger(VoiceResource.class.getName());
27  
28  	private VoicesWebServiceExtension provider;
29  	private String voiceID;
30  
31  	@SuppressWarnings("unchecked")
32  	@Override
33  	public void doInit() {
34  		super.doInit();
35  		if (!isAuthenticated()) {
36  			return;
37  		}
38  		provider = (VoicesWebServiceExtension)getContext().getAttributes().get(VoicesWebServiceExtension.CONTEXT_ATTRIBUTE_KEY);
39  		voiceID = (String)getRequestAttributes().get("id");
40  	}
41  
42  	/**
43  	 * Gets the resource.
44  	 *
45  	 * @return the resource
46  	 */
47  	@Get("xml")
48  	public Representation getResource() {
49  		logRequest();
50  		maybeEnableCORS();
51  		if (!isAuthenticated()) {
52  			setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
53  			return null;
54  		}
55  		VoiceManager voiceManager = provider.getRememberedVoiceManager(Properties.getSnapshot());
56  		if (voiceManager == null) {
57  			setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
58  			return getErrorRepresentation("No /voices call preceeded this call, or settings were changed");
59  		}
60  		Voice voice = voiceManager.getVoiceForID(voiceID);
61  		if (voice == null) {
62  			setStatus(Status.CLIENT_ERROR_NOT_FOUND);
63  			return getErrorRepresentation("Voice not found");
64  		}
65  		Document voiceDoc; {
66  			voiceDoc = XmlUtils.createDom("voice");
67  			Element voiceElem = voiceDoc.getDocumentElement();
68  			addElementData(voiceElem, getRequest().getRootRef().toString(), voice, false);
69  		}
70  		DomRepresentation dom; {
71  			try {
72  				dom = new DomRepresentation(MediaType.APPLICATION_XML, voiceDoc);
73  				setStatus(Status.SUCCESS_OK);
74  			} catch (Exception e) {
75  				setStatus(Status.SERVER_ERROR_INTERNAL);
76  				return getErrorRepresentation(e.getMessage());
77  			}
78  		}
79  		logResponse(dom);
80  		return dom;
81  	}
82  
83  	static void addElementData(Element voiceElem, String baseURL, Voice voice, boolean omitRefs) {
84  		if (!omitRefs)
85  			voiceElem.setAttribute("href", baseURL + VoicesWebServiceExtension.VOICE_ROUTE.replaceFirst("\\{id\\}", voice.getID()));
86  		voiceElem.setAttribute("name", voice.getName());
87  		voiceElem.setAttribute("engine", voice.getEngine());
88  		Collection<LanguageRange> locale = voice.getLocale();
89  		if (!locale.isEmpty()) {
90  			voiceElem.setAttribute("lang", LanguageRange.toString(voice.getLocale()));
91  		}
92  		if (voice.getGender().isPresent()) {
93  			voiceElem.setAttribute("gender", voice.getGender().get().toString());
94  		}
95  		if (!omitRefs)
96  		voiceElem.setAttribute(
97  			"preview", baseURL + VoicesWebServiceExtension.VOICE_PREVIEW_ROUTE.replaceFirst("\\{id\\}", voice.getID()));
98  	}
99  }