1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.domain; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.net.URI; |
24 | import java.util.List; |
25 | |
26 | import org.jclouds.util.Strings2; |
27 | |
28 | import com.google.common.base.Splitter; |
29 | import com.google.common.collect.Lists; |
30 | |
31 | /** |
32 | * @author Adrian Cole |
33 | */ |
34 | public class Credentials { |
35 | |
36 | public static class Builder<T extends Credentials> { |
37 | private String identity; |
38 | private String credential; |
39 | |
40 | public Builder<T> identity(String identity) { |
41 | this.identity = identity; |
42 | return this; |
43 | } |
44 | |
45 | public Builder<T> credential(String credential) { |
46 | this.credential = credential; |
47 | return this; |
48 | } |
49 | |
50 | @SuppressWarnings("unchecked") |
51 | public T build() { |
52 | return (T) new Credentials(identity, credential); |
53 | } |
54 | } |
55 | |
56 | public final String identity; |
57 | public final String credential; |
58 | |
59 | public Credentials(String identity, String credential) { |
60 | this.identity = identity; |
61 | this.credential = credential; |
62 | } |
63 | |
64 | public static Credentials parse(URI uri) { |
65 | checkNotNull(uri, "uri"); |
66 | List<String> userInfo = Lists.newArrayList(Splitter.on(':').split( |
67 | checkNotNull(uri.getUserInfo(), "no userInfo in " + uri))); |
68 | String identity = checkNotNull(userInfo.get(0), "no username in " + uri.getUserInfo()); |
69 | if (Strings2.isUrlEncoded(identity)) { |
70 | identity = Strings2.urlDecode(identity); |
71 | } |
72 | String credential = userInfo.size() > 1 ? userInfo.get(1) : null; |
73 | if (credential != null && Strings2.isUrlEncoded(credential)) { |
74 | credential = Strings2.urlDecode(credential); |
75 | } |
76 | return new Credentials(identity, credential); |
77 | } |
78 | |
79 | public Builder<? extends Credentials> toBuilder() { |
80 | return new Builder<Credentials>().identity(identity).credential(credential); |
81 | } |
82 | |
83 | @Override |
84 | public int hashCode() { |
85 | final int prime = 31; |
86 | int result = 1; |
87 | result = prime * result + ((credential == null) ? 0 : credential.hashCode()); |
88 | result = prime * result + ((identity == null) ? 0 : identity.hashCode()); |
89 | return result; |
90 | } |
91 | |
92 | @Override |
93 | public boolean equals(Object obj) { |
94 | if (this == obj) |
95 | return true; |
96 | if (obj == null) |
97 | return false; |
98 | if (!(obj instanceof Credentials)) |
99 | return false; |
100 | Credentials other = (Credentials) obj; |
101 | if (credential == null) { |
102 | if (other.credential != null) |
103 | return false; |
104 | } else if (!credential.equals(other.credential)) |
105 | return false; |
106 | if (identity == null) { |
107 | if (other.identity != null) |
108 | return false; |
109 | } else if (!identity.equals(other.identity)) |
110 | return false; |
111 | return true; |
112 | } |
113 | |
114 | } |