View Javadoc

1   /*
2    * Copyright (C) 2012 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  package com.atlassian.jira.rest.client.internal.json;
17  
18  import com.atlassian.jira.rest.client.api.domain.RoleActor;
19  import org.codehaus.jettison.json.JSONException;
20  import org.codehaus.jettison.json.JSONObject;
21  
22  import javax.ws.rs.core.UriBuilder;
23  import java.net.URI;
24  
25  
26  public class RoleActorJsonParser implements JsonObjectParser<RoleActor> {
27  
28  	private final URI baseJiraUri;
29  
30  	public RoleActorJsonParser(URI baseJiraUri) {
31  		this.baseJiraUri = baseJiraUri;
32  	}
33  
34  	@Override
35  	public RoleActor parse(final JSONObject json) throws JSONException {
36  		// Workaround for a bug in API. Id field should not be optional, unfortunately it is not returned for an admin role actor.
37  		final Long id = JsonParseUtil.getOptionalLong(json, "id");
38  		final String displayName = json.getString("displayName");
39  		final String type = json.getString("type");
40  		final String name = json.getString("name");
41  		return new RoleActor(id, displayName, type, name, parseAvatarUrl(json));
42  	}
43  
44  	private URI parseAvatarUrl(final JSONObject json) {
45  		final String pathToAvatar = JsonParseUtil.getOptionalString(json, "avatarUrl");
46  		if (pathToAvatar != null) {
47  			final URI avatarUri = UriBuilder.fromUri(pathToAvatar).build();
48  			return avatarUri.isAbsolute() ? avatarUri : baseJiraUri.resolve(pathToAvatar);
49  		} else {
50  			return null;
51  		}
52  	}
53  
54  }