View Javadoc

1   /*
2    * Copyright (C) 2010 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.jira.rest.client.internal.json;
18  
19  import com.atlassian.jira.rest.client.api.domain.BasicUser;
20  import com.atlassian.jira.rest.client.api.domain.User;
21  import com.atlassian.jira.rest.client.api.ExpandableProperty;
22  import com.google.common.base.Preconditions;
23  import com.google.common.collect.Maps;
24  import org.codehaus.jettison.json.JSONException;
25  import org.codehaus.jettison.json.JSONObject;
26  
27  import java.net.URI;
28  import java.util.Iterator;
29  import java.util.Map;
30  
31  public class UserJsonParser implements JsonObjectParser<User> {
32  	@Override
33  	public User parse(JSONObject json) throws JSONException {
34  		final BasicUser basicUser = Preconditions.checkNotNull(JsonParseUtil.parseBasicUser(json));
35  		final String timezone = JsonParseUtil.getOptionalString(json, "timeZone");
36  		final String avatarUrl = JsonParseUtil.getOptionalString(json, "avatarUrl");
37  		Map<String, URI> avatarUris = Maps.newHashMap();
38  		if (avatarUrl != null) {
39  			// JIRA prior 5.0
40  			final URI avatarUri = JsonParseUtil.parseURI(avatarUrl);
41  			avatarUris.put(User.S48_48, avatarUri);
42  		} else {
43  			// JIRA 5.0+
44  			final JSONObject avatarUrlsJson = json.getJSONObject("avatarUrls");
45  			@SuppressWarnings("unchecked")
46  			final Iterator<String> iterator = avatarUrlsJson.keys();
47  			while (iterator.hasNext()) {
48  				final String key = iterator.next();
49  				avatarUris.put(key, JsonParseUtil.parseURI(avatarUrlsJson.getString(key)));
50  			}
51  		}
52  		// e-mail may be not set in response if e-mail visibility in jira configuration is set to hidden (in jira 4.3+)
53  		final String emailAddress = JsonParseUtil.getOptionalString(json, "emailAddress");
54  		// optional because groups are not returned for issue->{reporter,assignee}
55  		final ExpandableProperty<String> groups = JsonParseUtil.parseOptionalExpandableProperty(json
56  				.optJSONObject("groups"), new JsonObjectParser<String>() {
57  			@Override
58  			public String parse(JSONObject json) throws JSONException {
59  				return json.getString("name");
60  			}
61  		});
62  		return new User(basicUser.getSelf(), basicUser.getName(), basicUser
63  				.getDisplayName(), emailAddress, groups, avatarUris, timezone);
64  	}
65  }