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