/******************************************************************************* * 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> { 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 parseFromJson(InputStreamReader in) { TypeToken> type = new TypeToken>() { }; return new GsonBuilder().registerTypeAdapter(type.getType(), new JSonTaskDataDeserializer()) .create() .fromJson(in, type.getType()); } BugzillaRestTaskSchema taskSchema = BugzillaRestTaskSchema.getDefault(); private class JSonTaskDataDeserializer implements JsonDeserializer> { @Override public ArrayList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { ArrayList response = new ArrayList(); for (Entry 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; } } }