1   /*
2    * Copyright 2017 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.internal.api.converter;
18  
19  import static com.google.common.base.Preconditions.checkArgument;
20  import static com.google.common.base.Strings.isNullOrEmpty;
21  import static java.util.Objects.requireNonNull;
22  
23  import java.lang.reflect.ParameterizedType;
24  
25  import javax.annotation.Nullable;
26  
27  import com.linecorp.armeria.common.AggregatedHttpRequest;
28  import com.linecorp.armeria.common.HttpStatus;
29  import com.linecorp.armeria.server.ServiceRequestContext;
30  import com.linecorp.armeria.server.annotation.RequestConverterFunction;
31  import com.linecorp.centraldogma.common.Author;
32  import com.linecorp.centraldogma.server.internal.admin.auth.AuthUtil;
33  import com.linecorp.centraldogma.server.internal.api.HttpApiUtil;
34  import com.linecorp.centraldogma.server.internal.storage.project.ProjectApiManager;
35  import com.linecorp.centraldogma.server.metadata.User;
36  import com.linecorp.centraldogma.server.storage.project.Project;
37  import com.linecorp.centraldogma.server.storage.repository.Repository;
38  
39  /**
40   * A default {@link RequestConverterFunction} of HTTP API.
41   */
42  public final class HttpApiRequestConverter implements RequestConverterFunction {
43  
44      private final ProjectApiManager projectApiManager;
45  
46      public HttpApiRequestConverter(ProjectApiManager projectApiManager) {
47          this.projectApiManager = requireNonNull(projectApiManager, "projectApiManager");
48      }
49  
50      @Override
51      public Object convertRequest(
52              ServiceRequestContext ctx, AggregatedHttpRequest request, Class<?> expectedResultType,
53              @Nullable ParameterizedType expectedParameterizedResultType) throws Exception {
54  
55          if (expectedResultType == Project.class) {
56              final String projectName = ctx.pathParam("projectName");
57              checkArgument(!isNullOrEmpty(projectName),
58                            "project name should not be null or empty.");
59  
60              // ProjectNotFoundException would be thrown if there is no project.
61              return projectApiManager.getProject(projectName);
62          }
63  
64          if (expectedResultType == Repository.class) {
65              final String projectName = ctx.pathParam("projectName");
66              checkArgument(!isNullOrEmpty(projectName),
67                            "project name should not be null or empty.");
68              final String repositoryName = ctx.pathParam("repoName");
69              checkArgument(!isNullOrEmpty(repositoryName),
70                            "repository name should not be null or empty.");
71  
72              if (Project.REPO_DOGMA.equals(repositoryName) &&
73                  !AuthUtil.currentUser(ctx).isAdmin()) {
74                  return HttpApiUtil.throwResponse(
75                          ctx, HttpStatus.FORBIDDEN,
76                          "Repository '%s/%s' can be accessed only by an administrator.",
77                          projectName, Project.REPO_DOGMA);
78              }
79              // RepositoryNotFoundException would be thrown if there is no project or no repository.
80              return projectApiManager.getProject(projectName).repos().get(repositoryName);
81          }
82  
83          if (expectedResultType == Author.class) {
84              return AuthUtil.currentAuthor(ctx);
85          }
86  
87          if (expectedResultType == User.class) {
88              return AuthUtil.currentUser(ctx);
89          }
90  
91          return RequestConverterFunction.fallthrough();
92      }
93  }