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.ExpandableProperty;
20  import com.atlassian.jira.rest.client.api.domain.BasicUser;
21  import com.atlassian.jira.rest.client.api.domain.User;
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") 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          // e-mail may be not set in response if e-mail visibility in jira configuration is set to hidden (in jira 4.3+)
52          final String emailAddress = JsonParseUtil.getOptionalString(json, "emailAddress");
53          // optional because groups are not returned for issue->{reporter,assignee}
54          final ExpandableProperty<String> groups = JsonParseUtil.parseOptionalExpandableProperty(json
55                  .optJSONObject("groups"), new JsonObjectParser<String>() {
56              @Override
57              public String parse(JSONObject json) throws JSONException {
58                  return json.getString("name");
59              }
60          });
61          return new User(basicUser.getSelf(), basicUser.getName(), basicUser
62                  .getDisplayName(), emailAddress, groups, avatarUris, timezone);
63      }
64  }