Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9c13aeffff3d2e92e410c73a00d3e1090b9afa92 (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
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
/*******************************************************************************
 * Copyright (c) 2015 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.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.repositories.core.RepositoryLocation;
import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient;
import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpResponse;
import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil;
import org.eclipse.mylyn.internal.bugzilla.rest.core.response.data.BugzillaRestIdResult;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.osgi.util.NLS;

import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

public class BugzillaRestPostNewTask extends BugzillaRestPostRequest<BugzillaRestIdResult> {
	TaskData taskData;

	public BugzillaRestPostNewTask(CommonHttpClient client, TaskData taskData) {
		super(client, "/bug"); //$NON-NLS-1$
		this.taskData = taskData;
	}

	List<NameValuePair> requestParameters;

	class TaskAttributeTypeAdapter extends TypeAdapter<TaskData> {
		RepositoryLocation location;

		public TaskAttributeTypeAdapter(RepositoryLocation location) {
			super();
			this.location = location;
		}

		@Override
		public void write(JsonWriter out, TaskData taskData) throws IOException {
			out.beginObject();
			addAuthenticationToGson(out, location);
			for (Object element : taskData.getRoot().getAttributes().values()) {
				TaskAttribute taskAttribute = (TaskAttribute) element;
				String id = taskAttribute.getId();
				String attributValue = BugzillaRestGsonUtil.convertString2GSonString(taskAttribute.getValue());
				if (legalCreateAttributes.contains(id) || id.startsWith("cf_")) { //$NON-NLS-1$
					id = BugzillaRestCreateTaskSchema.getFieldNameFromAttributeName(id);
					if (id.equals("cc")) { //$NON-NLS-1$
						HashSet<String> setNew = new HashSet<String>(
								Arrays.asList(taskAttribute.getValue().split("\\s*,\\s*"))); //$NON-NLS-1$
						BugzillaRestGsonUtil.buildArrayFromHash(out, id, setNew, false);
					} else if (id.equals(BugzillaRestCreateTaskSchema.getDefault().BLOCKS.getKey())
							|| id.equals(BugzillaRestCreateTaskSchema.getDefault().DEPENDS_ON.getKey())) {
						if (taskAttribute.getValues().size() > 1) {
							HashSet<String> setNew = new HashSet<String>(taskAttribute.getValues());
							BugzillaRestGsonUtil.buildArrayFromHash(out, id, setNew, true);
						} else {
							HashSet<String> setNew = new HashSet<String>(
									Arrays.asList(taskAttribute.getValue().split("\\s*,\\s*"))); //$NON-NLS-1$
							BugzillaRestGsonUtil.buildArrayFromHash(out, id, setNew, true);
						}
					} else {

						if (id.equals(BugzillaRestCreateTaskSchema.getDefault().KEYWORDS.getKey())) {
							attributValue = taskAttribute.getValues().toString();
							attributValue = attributValue.substring(1, attributValue.length() - 1);
						}

						if (taskAttribute.getMetaData().getType() != null
								&& taskAttribute.getMetaData().getType().equals(TaskAttribute.TYPE_MULTI_SELECT)) {
							List<String> values = taskAttribute.getValues();
							int ii = 0;
							attributValue = ""; //$NON-NLS-1$
							for (String string : values) {
								string = BugzillaRestGsonUtil.convertString2GSonString(string);
								attributValue += ((ii++ == 0 ? "" : ",") + string); //$NON-NLS-1$ //$NON-NLS-2$
							}
						}
						out.name(id).value(attributValue);
						if (id.equals("description")) { //$NON-NLS-1$
							TaskAttribute descriptionpri = taskAttribute.getAttribute(
									BugzillaRestCreateTaskSchema.getDefault().DESCRIPTION_IS_PRIVATE.getKey());
							Boolean descriptionprivalue = (descriptionpri != null)
									? (descriptionpri.getValue().equals("1")) //$NON-NLS-1$
									: false;
							out.name("comment_is_private").value(Boolean.toString(descriptionprivalue)); //$NON-NLS-1$
						}
					}
				}
			}
			out.endObject();
		}

		@Override
		public TaskData read(JsonReader in) throws IOException {
			throw new UnsupportedOperationException(
					"TaskAttributeTypeAdapter in BugzillaRestPostNewTask only supports write"); //$NON-NLS-1$
		}
	}

	@Override
	protected void addHttpRequestEntities(HttpRequestBase request) throws BugzillaRestException {
		super.addHttpRequestEntities(request);

		try {
			// set form parameters
			Gson gson = new GsonBuilder()
					.registerTypeAdapter(TaskData.class, new TaskAttributeTypeAdapter(getClient().getLocation()))
					.create();
			StringEntity requestEntity = new StringEntity(gson.toJson(taskData));
			((HttpPost) request).setEntity(requestEntity);
		} catch (UnsupportedEncodingException e) {
			throw new BugzillaRestException("could not build REST String", e); //$NON-NLS-1$
		}
	}

	@Override
	protected BugzillaRestIdResult parseFromJson(InputStreamReader in) {
		TypeToken<BugzillaRestIdResult> type = new TypeToken<BugzillaRestIdResult>() {
		};
		return new Gson().fromJson(in, type.getType());
	}

	protected BugzillaRestStatus parseErrorFromJson(InputStreamReader in) {
		TypeToken<BugzillaRestStatus> type = new TypeToken<BugzillaRestStatus>() {
		};
		return new Gson().fromJson(in, type.getType());
	}

	@Override
	protected void doValidate(CommonHttpResponse response, IOperationMonitor monitor)
			throws IOException, BugzillaRestException {
		int statusCode = response.getStatusCode();
		if (statusCode != HttpURLConnection.HTTP_BAD_REQUEST && statusCode != HttpURLConnection.HTTP_OK) {
			if (statusCode == HttpStatus.SC_NOT_FOUND) {
				throw new BugzillaRestResourceNotFoundException(
						NLS.bind("Requested resource ''{0}'' does not exist", response.getRequestPath())); //$NON-NLS-1$
			}
			throw new BugzillaRestException(NLS.bind("Unexpected response from Bugzilla REST server for ''{0}'': {1}", //$NON-NLS-1$
					response.getRequestPath(), HttpUtil.getStatusText(statusCode)));
		}

	}

	ImmutableList<String> legalCreateAttributes = new ImmutableList.Builder<String>()
			.add(BugzillaRestCreateTaskSchema.getDefault().PRODUCT.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().COMPONENT.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().SUMMARY.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().VERSION.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().DESCRIPTION.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().OS.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().PLATFORM.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().PRIORITY.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().SEVERITY.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().ALIAS.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().ASSIGNED_TO.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().QA_CONTACT.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().TARGET_MILESTONE.getKey())
			.add(TaskAttribute.OPERATION)
			.add(BugzillaRestCreateTaskSchema.getDefault().CC.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().BLOCKS.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().DEPENDS_ON.getKey())
			.add(BugzillaRestCreateTaskSchema.getDefault().KEYWORDS.getKey())
			.build();

	@Override
	protected BugzillaRestIdResult doProcess(CommonHttpResponse response, IOperationMonitor monitor)
			throws IOException, BugzillaRestException {
		InputStream is = response.getResponseEntityAsStream();
		InputStreamReader in = new InputStreamReader(is);
		switch (response.getStatusCode()) {
		case HttpURLConnection.HTTP_OK:
			return parseFromJson(in);
		default:
			BugzillaRestStatus status = parseErrorFromJson(in);
			throw new BugzillaRestException(
					NLS.bind("{2}  (status: {1} from {0})", new String[] { response.getRequestPath(), //$NON-NLS-1$
							HttpUtil.getStatusText(response.getStatusCode()), status.getMessage() }));
		}
	}
}

Back to the top