1 package org.springframework.security.oauth.examples.tonr.impl;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URI;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import javax.xml.parsers.ParserConfigurationException;
11 import javax.xml.parsers.SAXParser;
12 import javax.xml.parsers.SAXParserFactory;
13
14 import org.springframework.security.oauth.examples.tonr.SparklrException;
15 import org.springframework.security.oauth.examples.tonr.SparklrService;
16 import org.springframework.web.client.RestOperations;
17 import org.xml.sax.Attributes;
18 import org.xml.sax.SAXException;
19 import org.xml.sax.helpers.DefaultHandler;
20
21
22
23
24 public class SparklrServiceImpl implements SparklrService {
25
26 private String sparklrPhotoListURL;
27 private String sparklrTrustedMessageURL;
28 private String sparklrPhotoURLPattern;
29 private RestOperations sparklrRestTemplate;
30 private RestOperations trustedClientRestTemplate;
31
32 public List<String> getSparklrPhotoIds() throws SparklrException {
33 try {
34 InputStream photosXML = new ByteArrayInputStream(sparklrRestTemplate.getForObject(
35 URI.create(sparklrPhotoListURL), byte[].class));
36
37 final List<String> photoIds = new ArrayList<String>();
38 SAXParserFactory parserFactory = SAXParserFactory.newInstance();
39 parserFactory.setValidating(false);
40 parserFactory.setXIncludeAware(false);
41 parserFactory.setNamespaceAware(false);
42 SAXParser parser = parserFactory.newSAXParser();
43 parser.parse(photosXML, new DefaultHandler() {
44 @Override
45 public void startElement(String uri, String localName, String qName, Attributes attributes)
46 throws SAXException {
47 if ("photo".equals(qName)) {
48 photoIds.add(attributes.getValue("id"));
49 }
50 }
51 });
52 return photoIds;
53 } catch (IOException e) {
54 throw new IllegalStateException(e);
55 } catch (SAXException e) {
56 throw new IllegalStateException(e);
57 } catch (ParserConfigurationException e) {
58 throw new IllegalStateException(e);
59 }
60 }
61
62 public InputStream loadSparklrPhoto(String id) throws SparklrException {
63 return new ByteArrayInputStream(sparklrRestTemplate.getForObject(
64 URI.create(String.format(sparklrPhotoURLPattern, id)), byte[].class));
65 }
66
67 public String getTrustedMessage() {
68 return this.trustedClientRestTemplate.getForObject(URI.create(sparklrTrustedMessageURL), String.class);
69 }
70
71 public void setSparklrPhotoURLPattern(String sparklrPhotoURLPattern) {
72 this.sparklrPhotoURLPattern = sparklrPhotoURLPattern;
73 }
74
75 public void setSparklrPhotoListURL(String sparklrPhotoListURL) {
76 this.sparklrPhotoListURL = sparklrPhotoListURL;
77 }
78
79 public void setSparklrTrustedMessageURL(String sparklrTrustedMessageURL) {
80 this.sparklrTrustedMessageURL = sparklrTrustedMessageURL;
81 }
82
83 public void setSparklrRestTemplate(RestOperations sparklrRestTemplate) {
84 this.sparklrRestTemplate = sparklrRestTemplate;
85 }
86
87 public void setTrustedClientRestTemplate(RestOperations trustedClientRestTemplate) {
88 this.trustedClientRestTemplate = trustedClientRestTemplate;
89 }
90
91 }