forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchInterrupted.java
More file actions
282 lines (247 loc) · 9.1 KB
/
Copy pathBatchInterrupted.java
File metadata and controls
282 lines (247 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gdata.data.batch;
import com.google.gdata.util.common.xml.XmlWriter;
import com.google.gdata.client.CoreErrorDomain;
import com.google.gdata.data.Extension;
import com.google.gdata.data.ExtensionDescription;
import com.google.gdata.data.ExtensionPoint;
import com.google.gdata.data.ExtensionProfile;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.Namespaces;
import com.google.gdata.util.ParseException;
import com.google.gdata.util.ServiceException;
import com.google.gdata.util.XmlParser;
import org.xml.sax.Attributes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Entry extension for the element {@code <batch:interrupted>}, which marks
* the batch feed as having been aborted.
*
*
*/
public class BatchInterrupted extends ExtensionPoint
implements Extension, IBatchInterrupted {
private String reason;
private int totalCount;
private int successCount;
private int errorCount;
private String content;
private ContentType contentType;
/**
* Creates and initializes a BatchInterrupted object.
*
* @param reason exception that caused batch processing to stop
* @param totalCount number of entries parsed so far, note that
* it is to be expected that {@code totalCount >= successCount + errorCount}
* @param successCount number of entries processed successfully so far
* @param errorCount number of entries rejected so far
*/
public BatchInterrupted(String reason,
int totalCount,
int successCount,
int errorCount) {
this.reason = reason;
this.totalCount = totalCount;
this.successCount = successCount;
this.errorCount = errorCount;
if (totalCount < (successCount - errorCount)) {
throw new IllegalArgumentException("total < success + error. total = " +
totalCount + " success=" + successCount + " error=" + errorCount);
}
}
/**
* Creates an empty object.
*
* Usually used in conjuction with
* {@link #getHandler(ExtensionProfile,String,String,Attributes)}.
*/
public BatchInterrupted() {
}
/**
* Creates and initializes a BatchInterrupted object.
*
* @param cause exception that caused batch processing to stop
* @param totalCount number of entries parsed so far, note that
* it is to be expected that {@code totalCount >= successCount + errorCount}
* @param successCount number of entries processed successfully so far
* @param errorCount number of entries rejected so far
*/
public BatchInterrupted(Throwable cause, int totalCount, int successCount,
int errorCount) {
this(getReasonFromException(cause), totalCount, successCount, errorCount);
if (cause instanceof ServiceException) {
ServiceException se = (ServiceException)cause;
content = se.getResponseBody();
contentType = se.getResponseContentType();
}
}
/** Returns the suggested extension description. */
public static ExtensionDescription getDefaultDescription() {
ExtensionDescription desc = new ExtensionDescription();
desc.setExtensionClass(BatchInterrupted.class);
desc.setNamespace(Namespaces.batchNs);
desc.setLocalName("interrupted");
desc.setRepeatable(false);
return desc;
}
private static String getReasonFromException(Throwable cause) {
String message = cause.getMessage();
if (message == null) {
return "Unexpected error";
} else {
return message;
}
}
/** Gets a short message describing what happened. */
public String getReason() {
return reason;
}
/** Gets the total number of entries read. */
public int getTotalCount() {
return totalCount;
}
/** Gets the number of entries that were processed successfully. */
public int getSuccessCount() {
return successCount;
}
/** Gets the number of entries that were rejected. */
public int getErrorCount() {
return errorCount;
}
/** Gets the number of entries that were skipped. */
public int getSkippedCount() {
return (totalCount - ( successCount + errorCount));
}
/** Describe the content of this tag. */
public ContentType getContentType() {
return contentType;
}
/** Sets the content type for this tag. */
public void setContentType(ContentType contentType) {
this.contentType = contentType;
}
/** Gets this tag content. See also {@link #getContentType()}. */
public String getContent() {
return content;
}
/** Sets this tag content. The type must correspond {@code contentType}. */
public void setContent(String content) {
this.content = content;
}
/**
* Generates an XML representation for batch:interrupted.
*/
@Override
public void generate(XmlWriter w, ExtensionProfile extProfile)
throws IOException {
List<XmlWriter.Attribute> attrs =
new ArrayList<XmlWriter.Attribute>(6);
if (reason != null) {
attrs.add(new XmlWriter.Attribute("reason", reason));
}
attrs.add(new XmlWriter.Attribute("parsed",
Integer.toString(totalCount)));
attrs.add(new XmlWriter.Attribute("success",
Integer.toString(successCount)));
attrs.add(new XmlWriter.Attribute("error",
Integer.toString(errorCount)));
int skippedCount = totalCount - ( successCount + errorCount);
attrs.add(new XmlWriter.Attribute("unprocessed",
Integer.toString(skippedCount)));
if (contentType != null) {
// Charset makes no sense in this context
contentType.getAttributes().remove(ContentType.ATTR_CHARSET);
attrs.add(new XmlWriter.Attribute("content-type",
contentType.toString()));
}
generateStartElement(w, Namespaces.batchNs, "interrupted", attrs, null);
// Invoke ExtensionPoint
generateExtensions(w, extProfile);
if (content != null) {
w.characters(content);
}
w.endElement(Namespaces.batchNs, "interrupted");
}
/**
* Creates an XML ElementHandler that will initialize the object based
* on a tag batch:interrupted parsed by the XML parser.
*
* @param extProfile
* @param namespace
* @param localName
* @param attrs attributes of batch:interrupted
* @return a child handler linked to the current object
* @throws ParseException
*/
@Override
public XmlParser.ElementHandler getHandler(ExtensionProfile extProfile,
String namespace, String localName,
Attributes attrs)
throws ParseException {
return new BatchInterruptedElementHandler(extProfile, attrs);
}
private static int getIntegerAttribute(Attributes attrs,
String name,
int defValue) throws ParseException {
String stringValue = attrs.getValue(name);
if (stringValue == null) {
return defValue;
}
try {
return Integer.parseInt(stringValue);
} catch (NumberFormatException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidIntegerAttribute, e);
pe.setInternalReason("Invalid integer in value of attribute "
+ name + ": '" + stringValue + "'");
throw pe;
}
}
/**
* Parses a batch:interrupted element and initializes the
* current object with what is found.
*/
private class BatchInterruptedElementHandler
extends ExtensionPoint.ExtensionHandler {
public BatchInterruptedElementHandler(ExtensionProfile extProfile,
Attributes attrs)
throws ParseException {
super(extProfile, BatchInterrupted.class);
totalCount = getIntegerAttribute(attrs, "parsed", 0);
successCount = getIntegerAttribute(attrs, "success", 0);
errorCount = getIntegerAttribute(attrs, "error", 0);
reason = attrs.getValue("reason");
String contentTypeString = attrs.getValue("content-type");
if (contentTypeString != null) {
try {
contentType = new ContentType(contentTypeString);
} catch (IllegalArgumentException e) {
ParseException pe = new ParseException(
CoreErrorDomain.ERR.invalidContentType, e);
pe.setInternalReason("Invalid content type: '" +
contentTypeString + "'");
throw pe;
}
}
}
@Override
public void processEndElement() {
content = value;
}
}
}