blob: d99d2faa78aede9ed0a554b4802bc0a8470697ac (
plain) (
blame)
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
|
/*******************************************************************************
* Copyright (c) 2016 Frank Becker and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Frank Becker - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.internal.bugzilla.rest.core;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import org.apache.commons.codec.binary.Base64InputStream;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
public class BugzillaRestGetTaskAttachmentData extends BugzillaRestGetRequest<InputStream> {
private final TaskAttribute taskAttribute;
public BugzillaRestGetTaskAttachmentData(CommonHttpClient client, TaskAttribute taskAttribute) {
super(client, "/bug/attachment/" + taskAttribute.getValue() + "?include_fields=data", null); //$NON-NLS-1$ //$NON-NLS-2$
this.taskAttribute = taskAttribute;
}
@Override
protected InputStream parseFromJson(InputStreamReader in) {
TypeToken<InputStream> type = new TypeToken<InputStream>() {
};
return new GsonBuilder().registerTypeAdapter(type.getType(), new JSonTaskDataDeserializer())
.create()
.fromJson(in, type.getType());
}
private class JSonTaskDataDeserializer implements JsonDeserializer<InputStream> {
@Override
public InputStream deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonElement attachments = ((JsonObject) json.getAsJsonObject().get("attachments")) //$NON-NLS-1$
.get(taskAttribute.getValue());
JsonPrimitive attachment = attachments.getAsJsonObject().get("data").getAsJsonPrimitive(); //$NON-NLS-1$
if (attachment == null) {
throw com.google.common.base.Throwables.propagate(new CoreException(
new Status(IStatus.ERROR, BugzillaRestCore.ID_PLUGIN, "Can not get Attachment Data"))); //$NON-NLS-1$
}
InputStream is = new ByteArrayInputStream(attachment.getAsString().getBytes());
return new Base64InputStream(is);
}
}
}
|