001
014
015 package com.liferay.portal.googleapps;
016
017 import com.liferay.portal.kernel.googleapps.GoogleAppsException;
018 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.servlet.HttpHeaders;
022 import com.liferay.portal.kernel.util.ContentTypes;
023 import com.liferay.portal.kernel.util.Http;
024 import com.liferay.portal.kernel.util.HttpUtil;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.xml.Attribute;
028 import com.liferay.portal.kernel.xml.Document;
029 import com.liferay.portal.kernel.xml.DocumentException;
030 import com.liferay.portal.kernel.xml.Element;
031 import com.liferay.portal.kernel.xml.Namespace;
032 import com.liferay.portal.kernel.xml.QName;
033 import com.liferay.portal.kernel.xml.SAXReaderUtil;
034
035 import java.io.IOException;
036
037 import java.util.List;
038
039
042 public class GHelperUtil {
043
044 public static final String APPS_URL =
045 "https:
046
047 public static Element addAppsProperty(
048 Element parentElement, String name, String value) {
049
050 Element element = parentElement.addElement("apps:property");
051
052 element.addAttribute("name", name);
053 element.addAttribute("value", value);
054
055 return element;
056 }
057
058 public static Element addAtomCategory(Element parentElement, String type) {
059 Element element = parentElement.addElement("atom:category");
060
061 element.addAttribute("scheme", "http:
062 element.addAttribute(
063 "term", "http:
064
065 return element;
066 }
067
068 public static Element addAtomEntry(Document document) {
069 Element element = document.addElement("atom:entry");
070
071 element.add(getAppsNamespace());
072 element.add(getAtomNamespace());
073
074 return element;
075 }
076
077 public static Namespace getAppsNamespace() {
078 return SAXReaderUtil.createNamespace(_APPS_PREFIX, _APPS_URI);
079 }
080
081 public static QName getAppsQName(String localName) {
082 return SAXReaderUtil.createQName(localName, getAppsNamespace());
083 }
084
085 public static Namespace getAtomNamespace() {
086 return SAXReaderUtil.createNamespace(_ATOM_PREFIX, _ATOM_URI);
087 }
088
089 public static QName getAtomQName(String localName) {
090 return SAXReaderUtil.createQName(localName, getAtomNamespace());
091 }
092
093 public static Document getDocument(
094 GAuthenticator gAuthenticator, String url)
095 throws GoogleAppsException {
096
097 try {
098 if (_log.isInfoEnabled()) {
099 _log.info("getDocument request " + url);
100 }
101
102 Http.Options options = _getOptions(gAuthenticator);
103
104 options.setLocation(url);
105
106 String xml = HttpUtil.URLtoString(options);
107
108 if (_log.isInfoEnabled()) {
109 _log.info("getDocument response " + xml);
110 }
111
112 return SAXReaderUtil.read(new UnsyncStringReader(xml));
113 }
114 catch (DocumentException de) {
115 throw new GoogleAppsException(de);
116 }
117 catch (IOException ioe) {
118 throw new GoogleAppsException(ioe);
119 }
120 }
121
122 public static String getErrorMessage(Document document) {
123 Element rootElement = document.getRootElement();
124
125 Element errorElement = rootElement.element("error");
126
127 List<Attribute> attributes = errorElement.attributes();
128
129 StringBundler sb = new StringBundler(attributes.size() * 4 + 1);
130
131 sb.append(StringPool.OPEN_CURLY_BRACE);
132
133 for (int i = 0; i < attributes.size(); i++) {
134 Attribute attribute = attributes.get(i);
135
136 sb.append(attribute.getName());
137 sb.append(StringPool.EQUAL);
138 sb.append(attribute.getValue());
139
140 if ((i + 1) <= attributes.size()) {
141 sb.append(StringPool.COMMA_AND_SPACE);
142 }
143 }
144
145 sb.append(StringPool.CLOSE_CURLY_BRACE);
146
147 return sb.toString();
148 }
149
150 public static boolean hasError(Document document) {
151 Element rootElement = document.getRootElement();
152
153 if (rootElement.element("error") != null) {
154 return true;
155 }
156 else {
157 return false;
158 }
159 }
160
161 public static void submitAdd(
162 GAuthenticator gAuthenticator, String url, Document document)
163 throws GoogleAppsException {
164
165 try {
166 String body = document.formattedString();
167
168 if (_log.isInfoEnabled()) {
169 _log.info("submitAdd request url " + url);
170 _log.info("submitAdd request body " + body);
171 }
172
173 Http.Options options = _getOptions(gAuthenticator);
174
175 options.setBody(
176 body, ContentTypes.APPLICATION_ATOM_XML, StringPool.UTF8);
177 options.setLocation(url);
178 options.setPost(true);
179
180 String response = HttpUtil.URLtoString(options);
181
182 if (_log.isInfoEnabled()) {
183 _log.info("submitAdd response " + response);
184 }
185 }
186 catch (IOException ioe) {
187 throw new GoogleAppsException(ioe);
188 }
189 }
190
191 public static void submitDelete(GAuthenticator gAuthenticator, String url)
192 throws GoogleAppsException {
193
194 try {
195 if (_log.isInfoEnabled()) {
196 _log.info("submitDelete request " + url);
197 }
198
199 Http.Options options = _getOptions(gAuthenticator);
200
201 options.setDelete(true);
202 options.setLocation(url);
203
204 String response = HttpUtil.URLtoString(options);
205
206 if (_log.isInfoEnabled()) {
207 _log.info("submitDelete response " + response);
208 }
209 }
210 catch (IOException ioe) {
211 throw new GoogleAppsException(ioe);
212 }
213 }
214
215 public static void submitUpdate(
216 GAuthenticator gAuthenticator, String url, Document document)
217 throws GoogleAppsException {
218
219 try {
220 String body = document.formattedString();
221
222 if (_log.isInfoEnabled()) {
223 _log.info("submitUpdate request url " + url);
224 _log.info("submitUpdate request body " + body);
225 }
226
227 Http.Options options = _getOptions(gAuthenticator);
228
229 options.setBody(
230 body, ContentTypes.APPLICATION_ATOM_XML, StringPool.UTF8);
231 options.setLocation(url);
232 options.setPut(true);
233
234 String response = HttpUtil.URLtoString(options);
235
236 if (_log.isInfoEnabled()) {
237 _log.info("submitUpdate response " + response);
238 }
239 }
240 catch (IOException ioe) {
241 throw new GoogleAppsException(ioe);
242 }
243 }
244
245 private static Http.Options _getOptions(GAuthenticator gAuthenticator) {
246 Http.Options options = new Http.Options();
247
248 options.addHeader(
249 HttpHeaders.AUTHORIZATION,
250 "GoogleLogin auth=" + gAuthenticator.getAuthenticationToken());
251
252 return options;
253 }
254
255 private static final String _APPS_PREFIX = "apps";
256
257 private static final String _APPS_URI =
258 "http:
259
260 private static final String _ATOM_PREFIX = "atom";
261
262 private static final String _ATOM_URI = "http:
263
264 private static Log _log = LogFactoryUtil.getLog(GHelperUtil.class);
265
266 }