1
14
15 package com.liferay.portal.googleapps;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
18 import com.liferay.portal.kernel.servlet.HttpHeaders;
19 import com.liferay.portal.kernel.util.ContentTypes;
20 import com.liferay.portal.kernel.util.Http;
21 import com.liferay.portal.kernel.util.HttpUtil;
22 import com.liferay.portal.kernel.util.StringBundler;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.kernel.xml.Attribute;
25 import com.liferay.portal.kernel.xml.Document;
26 import com.liferay.portal.kernel.xml.DocumentException;
27 import com.liferay.portal.kernel.xml.Element;
28 import com.liferay.portal.kernel.xml.Namespace;
29 import com.liferay.portal.kernel.xml.QName;
30 import com.liferay.portal.kernel.xml.SAXReaderUtil;
31
32 import java.io.IOException;
33
34 import java.util.List;
35
36
41 public class GHelperUtil {
42
43 public static final String APPS_URL =
44 "https://apps-apis.google.com/a/feeds";
45
46 public static Element addAppsProperty(
47 Element parentElement, String name, String value) {
48
49 Element element = parentElement.addElement("apps:property");
50
51 element.addAttribute("name", name);
52 element.addAttribute("value", value);
53
54 return element;
55 }
56
57 public static Element addAtomCategory(Element parentElement, String type) {
58 Element element = parentElement.addElement("atom:category");
59
60 element.addAttribute("scheme", "http://schemas.google.com/g/2005#kind");
61 element.addAttribute(
62 "term", "http://schemas.google.com/apps/2006#" + type);
63
64 return element;
65 }
66
67 public static Element addAtomEntry(Document document) {
68 Element element = document.addElement("atom:entry");
69
70 element.add(getAppsNamespace());
71 element.add(getAtomNamespace());
72
73 return element;
74 }
75
76 public static Namespace getAppsNamespace() {
77 return SAXReaderUtil.createNamespace(_APPS_PREFIX, _APPS_URI);
78 }
79
80 public static QName getAppsQName(String localName) {
81 return SAXReaderUtil.createQName(localName, getAppsNamespace());
82 }
83
84 public static Namespace getAtomNamespace() {
85 return SAXReaderUtil.createNamespace(_ATOM_PREFIX, _ATOM_URI);
86 }
87
88 public static QName getAtomQName(String localName) {
89 return SAXReaderUtil.createQName(localName, getAtomNamespace());
90 }
91
92 public static Document getDocument(
93 GAuthenticator gAuthenticator, String url)
94 throws GoogleAppsException {
95
96 try {
97 Http.Options options = _getOptions(gAuthenticator);
98
99 options.setLocation(url);
100
101 String xml = HttpUtil.URLtoString(options);
102
103 return SAXReaderUtil.read(new UnsyncStringReader(xml));
104 }
105 catch (DocumentException de) {
106 throw new GoogleAppsException(de);
107 }
108 catch (IOException ioe) {
109 throw new GoogleAppsException(ioe);
110 }
111 }
112
113 public static String getErrorMessage(Document document) {
114 Element rootElement = document.getRootElement();
115
116 Element errorElement = rootElement.element("error");
117
118 List<Attribute> attributes = errorElement.attributes();
119
120 StringBundler sb = new StringBundler(attributes.size() * 4 + 1);
121
122 sb.append(StringPool.OPEN_CURLY_BRACE);
123
124 for (int i = 0; i < attributes.size(); i++) {
125 Attribute attribute = attributes.get(i);
126
127 sb.append(attribute.getName());
128 sb.append(StringPool.EQUAL);
129 sb.append(attribute.getValue());
130
131 if ((i + 1) <= attributes.size()) {
132 sb.append(StringPool.COMMA_AND_SPACE);
133 }
134 }
135
136 sb.append(StringPool.CLOSE_CURLY_BRACE);
137
138 return sb.toString();
139 }
140
141 public static boolean hasError(Document document) {
142 Element rootElement = document.getRootElement();
143
144 if (rootElement.element("error") != null) {
145 return true;
146 }
147 else {
148 return false;
149 }
150 }
151
152 public static void submitAdd(
153 GAuthenticator gAuthenticator, String url, Document document)
154 throws GoogleAppsException {
155
156 try {
157 Http.Options options = _getOptions(gAuthenticator);
158
159 options.setBody(
160 document.formattedString(), ContentTypes.APPLICATION_ATOM_XML,
161 StringPool.UTF8);
162 options.setLocation(url);
163 options.setPost(true);
164
165 HttpUtil.URLtoString(options);
166 }
167 catch (IOException ioe) {
168 throw new GoogleAppsException(ioe);
169 }
170 }
171
172 public static void submitDelete(GAuthenticator gAuthenticator, String url)
173 throws GoogleAppsException {
174
175 try {
176 Http.Options options = _getOptions(gAuthenticator);
177
178 options.setDelete(true);
179 options.setLocation(url);
180
181 HttpUtil.URLtoString(options);
182 }
183 catch (IOException ioe) {
184 throw new GoogleAppsException(ioe);
185 }
186 }
187
188 public static void submitUpdate(
189 GAuthenticator gAuthenticator, String url, Document document)
190 throws GoogleAppsException {
191
192 try {
193 Http.Options options = _getOptions(gAuthenticator);
194
195 options.setBody(
196 document.formattedString(), ContentTypes.APPLICATION_ATOM_XML,
197 StringPool.UTF8);
198 options.setLocation(url);
199 options.setPut(true);
200
201 HttpUtil.URLtoString(options);
202 }
203 catch (IOException ioe) {
204 throw new GoogleAppsException(ioe);
205 }
206 }
207
208 private static Http.Options _getOptions(GAuthenticator gAuthenticator) {
209 Http.Options options = new Http.Options();
210
211 options.addHeader(
212 HttpHeaders.AUTHORIZATION,
213 "GoogleLogin auth=" + gAuthenticator.getAuthenticationToken());
214
215 return options;
216 }
217
218 private static final String _APPS_PREFIX = "apps";
219
220 private static final String _APPS_URI =
221 "http://schemas.google.com/apps/2006";
222
223 private static final String _ATOM_PREFIX = "atom";
224
225 private static final String _ATOM_URI = "http://www.w3.org/2005/Atom";
226
227 }