Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7782f92a9019a14e329cb0cdd60c5226206799ad (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
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
/*******************************************************************************
 * 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.InputStreamReader;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.TimeZone;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.tasks.core.IRepositoryPerson;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;

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.reflect.TypeToken;

public class BugzillaRestGetTaskAttachments extends BugzillaRestAuthenticatedGetRequest<ArrayList<TaskAttribute>> {
	private final TaskData taskData;

	public BugzillaRestGetTaskAttachments(BugzillaRestHttpClient client, TaskData taskData) {
		super(client, "/bug/" + taskData.getTaskId() + "/attachment?exclude_fields=data", null); //$NON-NLS-1$ //$NON-NLS-2$
		this.taskData = taskData;
	}

	@Override
	protected ArrayList<TaskAttribute> parseFromJson(InputStreamReader in) {
		TypeToken<ArrayList<TaskAttribute>> type = new TypeToken<ArrayList<TaskAttribute>>() {
		};
		return new GsonBuilder().registerTypeAdapter(type.getType(), new JSonTaskDataDeserializer())
				.create()
				.fromJson(in, type.getType());
	}

	BugzillaRestTaskSchema taskSchema = BugzillaRestTaskSchema.getDefault();

	private class JSonTaskDataDeserializer implements JsonDeserializer<ArrayList<TaskAttribute>> {

		@Override
		public ArrayList<TaskAttribute> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
				throws JsonParseException {
			ArrayList<TaskAttribute> response = new ArrayList<TaskAttribute>();

			for (Entry<String, JsonElement> bugEntry : ((JsonObject) json.getAsJsonObject().get("bugs")).entrySet()) { //$NON-NLS-1$
				for (JsonElement jsonElement : bugEntry.getValue().getAsJsonArray()) {
					JsonObject attachmentObject = (JsonObject) jsonElement;
					String id = attachmentObject.get("id").getAsString(); //$NON-NLS-1$
					String creator = attachmentObject.get("creator").getAsString(); //$NON-NLS-1$
					Long size = attachmentObject.get("size").getAsLong(); //$NON-NLS-1$
					TaskAttribute attachmentAttribute = taskData.getRoot()
							.createAttribute(TaskAttribute.PREFIX_ATTACHMENT + id);
					BugzillaRestAttachmentMapper attachmentMapper = BugzillaRestAttachmentMapper
							.createFrom(attachmentAttribute);
					attachmentMapper.setAttachmentId(id);

					IRepositoryPerson author = taskData.getAttributeMapper().getTaskRepository().createPerson(creator);
					author.setName(creator);
					attachmentMapper.setAuthor(author);
					attachmentMapper.setLength(size != null ? size : -1L);
					try {
						SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); //$NON-NLS-1$
						iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
						Date tempDate = iso8601Format.parse(attachmentObject.get("creation_time").getAsString()); //$NON-NLS-1$
						attachmentMapper.setCreationDate(tempDate);
						tempDate = iso8601Format.parse(attachmentObject.get("last_change_time").getAsString()); //$NON-NLS-1$
						attachmentMapper.setDeltaDate(tempDate);
					} catch (ParseException e) {
						com.google.common.base.Throwables.propagate(new CoreException(new Status(IStatus.ERROR,
								BugzillaRestCore.ID_PLUGIN,
								"Can not parse Date (" + attachmentObject.get("creation_time").getAsString() + ")"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					}
					attachmentMapper.setContentType(attachmentObject.get("content_type").getAsString()); //$NON-NLS-1$
					attachmentMapper.setDeprecated(attachmentObject.get("is_obsolete") //$NON-NLS-1$
							.getAsString()
							.equals(Integer.valueOf(1)));
					attachmentMapper.setDescription(attachmentObject.get("summary").getAsString()); //$NON-NLS-1$
					attachmentMapper.setFileName(attachmentObject.get("file_name").getAsString()); //$NON-NLS-1$
					attachmentMapper
							.setPatch(attachmentObject.get("is_patch").getAsString().equals(Integer.valueOf(1))); //$NON-NLS-1$
					attachmentMapper.applyTo(attachmentAttribute);
				}
			}
			return response;
		}

	}

}

Back to the top