1   /*
2    * Copyright 2019 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.metadata;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import com.fasterxml.jackson.annotation.JsonCreator;
22  import com.fasterxml.jackson.annotation.JsonInclude;
23  import com.fasterxml.jackson.annotation.JsonInclude.Include;
24  import com.fasterxml.jackson.annotation.JsonProperty;
25  import com.google.common.base.MoreObjects;
26  
27  import com.linecorp.centraldogma.common.ProjectRole;
28  import com.linecorp.centraldogma.server.storage.repository.HasWeight;
29  
30  /**
31   * Specifies a registration of a {@link Token}.
32   */
33  @JsonInclude(Include.NON_NULL)
34  public class TokenRegistration implements Identifiable, HasWeight {
35  
36      /**
37       * An application identifier which belongs to a {@link Token}.
38       */
39      private final String appId;
40  
41      /**
42       * A role of the {@link Token} in a project.
43       */
44      private final ProjectRole role;
45  
46      /**
47       * Specifies when the token is registered by whom.
48       */
49      private final UserAndTimestamp creation;
50  
51      /**
52       * Creates a new instance.
53       */
54      @JsonCreator
55      public TokenRegistration(@JsonProperty("appId") String appId,
56                               @JsonProperty("role") ProjectRole role,
57                               @JsonProperty("creation") UserAndTimestamp creation) {
58          this.appId = requireNonNull(appId, "appId");
59          this.role = requireNonNull(role, "role");
60          this.creation = requireNonNull(creation, "creation");
61      }
62  
63      @Override
64      public String id() {
65          return appId;
66      }
67  
68      /**
69       * Returns the ID of the application.
70       */
71      @JsonProperty
72      public String appId() {
73          return appId;
74      }
75  
76      /**
77       * Returns the role of the token in a project.
78       */
79      @JsonProperty
80      public ProjectRole role() {
81          return role;
82      }
83  
84      /**
85       * Returns who creates the token when.
86       */
87      @JsonProperty
88      public UserAndTimestamp creation() {
89          return creation;
90      }
91  
92      @Override
93      public int weight() {
94          return appId.length() + role.name().length();
95      }
96  
97      @Override
98      public String toString() {
99          return MoreObjects.toStringHelper(this)
100                           .add("appId", appId())
101                           .add("role", role())
102                           .add("creation", creation())
103                           .toString();
104     }
105 }