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.ExpandableProperty;
20  import com.atlassian.jira.rest.client.domain.BasicUser;
21  import com.atlassian.jira.rest.client.domain.User;
22  import com.google.common.collect.Maps;
23  import org.codehaus.jettison.json.JSONException;
24  import org.codehaus.jettison.json.JSONObject;
25  
26  import java.net.URI;
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  public class UserJsonParser implements JsonParser<User> {
31  	@Override
32  	public User parse(JSONObject json) throws JSONException {
33  		final BasicUser basicUser = JsonParseUtil.parseBasicUser(json);
34  		final String timezone = JsonParseUtil.getOptionalString(json, "timeZone");
35  		final String avatarUrl = JsonParseUtil.getOptionalString(json, "avatarUrl");
36  		Map<String, URI> avatarUris = Maps.newHashMap();
37  		if (avatarUrl != null) {
38  			// JIRA prior 5.0
39  			final URI avatarUri = JsonParseUtil.parseURI(avatarUrl);
40  			avatarUris.put(User.S48_48, avatarUri);
41  		} else {
42  			// JIRA 5.0+
43  			final JSONObject avatarUrlsJson = json.getJSONObject("avatarUrls");
44  			@SuppressWarnings("unchecked")
45  			final Iterator<String> iterator = avatarUrlsJson.keys();
46  			while (iterator.hasNext()) {
47  				final String key = iterator.next();
48  				avatarUris.put(key, JsonParseUtil.parseURI(avatarUrlsJson.getString(key)));
49  			}
50  		}
51  		final String emailAddress = json.getString("emailAddress");
52  		// we expect always expanded groups, serving them is anyway cheap - that was the case for JIRA prior 5.0, now groups are not expanded...
53  		final ExpandableProperty<String> groups = JsonParseUtil.parseExpandableProperty(json.getJSONObject("groups"), new JsonParser<String>() {
54  			@Override
55  			public String parse(JSONObject json) throws JSONException {
56  				return json.getString("name");
57  			}
58  		});
59  		return new User(basicUser.getSelf(), basicUser.getName(), basicUser.getDisplayName(), emailAddress, groups, avatarUris, timezone);
60  	}
61  }