Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.api.projects; 16 : 17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException; 18 : 19 : import com.google.gerrit.exceptions.StorageException; 20 : import com.google.gerrit.extensions.api.projects.ProjectApi; 21 : import com.google.gerrit.extensions.api.projects.ProjectInput; 22 : import com.google.gerrit.extensions.api.projects.Projects; 23 : import com.google.gerrit.extensions.common.ProjectInfo; 24 : import com.google.gerrit.extensions.restapi.BadRequestException; 25 : import com.google.gerrit.extensions.restapi.RestApiException; 26 : import com.google.gerrit.extensions.restapi.UnprocessableEntityException; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.restapi.project.ListProjects; 29 : import com.google.gerrit.server.restapi.project.ListProjects.FilterType; 30 : import com.google.gerrit.server.restapi.project.ProjectsCollection; 31 : import com.google.gerrit.server.restapi.project.QueryProjects; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Provider; 34 : import com.google.inject.Singleton; 35 : import java.util.List; 36 : import java.util.SortedMap; 37 : 38 : @Singleton 39 : class ProjectsImpl implements Projects { 40 : private final ProjectsCollection projects; 41 : private final ProjectApiImpl.Factory api; 42 : private final Provider<ListProjects> listProvider; 43 : private final Provider<QueryProjects> queryProvider; 44 : 45 : @Inject 46 : ProjectsImpl( 47 : ProjectsCollection projects, 48 : ProjectApiImpl.Factory api, 49 : Provider<ListProjects> listProvider, 50 149 : Provider<QueryProjects> queryProvider) { 51 149 : this.projects = projects; 52 149 : this.api = api; 53 149 : this.listProvider = listProvider; 54 149 : this.queryProvider = queryProvider; 55 149 : } 56 : 57 : @Override 58 : public ProjectApi name(String name) throws RestApiException { 59 : try { 60 66 : return api.create(projects.parse(name)); 61 143 : } catch (UnprocessableEntityException e) { 62 143 : return api.create(name); 63 0 : } catch (Exception e) { 64 0 : throw asRestApiException("Cannot retrieve project", e); 65 : } 66 : } 67 : 68 : @Override 69 : public ProjectApi create(String name) throws RestApiException { 70 12 : ProjectInput in = new ProjectInput(); 71 12 : in.name = name; 72 12 : return create(in); 73 : } 74 : 75 : @Override 76 : public ProjectApi create(ProjectInput in) throws RestApiException { 77 143 : if (in.name == null) { 78 1 : throw new BadRequestException("input.name is required"); 79 : } 80 143 : return name(in.name).create(in); 81 : } 82 : 83 : @Override 84 : public ListRequest list() { 85 3 : return new ListRequest() { 86 : @Override 87 : public SortedMap<String, ProjectInfo> getAsMap() throws RestApiException { 88 : try { 89 3 : return list(this); 90 1 : } catch (Exception e) { 91 1 : throw asRestApiException("project list unavailable", e); 92 : } 93 : } 94 : }; 95 : } 96 : 97 : private SortedMap<String, ProjectInfo> list(ListRequest request) 98 : throws RestApiException, PermissionBackendException { 99 3 : ListProjects lp = listProvider.get(); 100 3 : lp.setShowDescription(request.getDescription()); 101 3 : lp.setLimit(request.getLimit()); 102 3 : lp.setStart(request.getStart()); 103 3 : lp.setMatchPrefix(request.getPrefix()); 104 : 105 3 : lp.setMatchSubstring(request.getSubstring()); 106 3 : lp.setMatchRegex(request.getRegex()); 107 3 : lp.setShowTree(request.getShowTree()); 108 3 : for (String branch : request.getBranches()) { 109 1 : lp.addShowBranch(branch); 110 1 : } 111 : 112 : FilterType type; 113 3 : switch (request.getFilterType()) { 114 : case ALL: 115 3 : type = FilterType.ALL; 116 3 : break; 117 : case CODE: 118 0 : type = FilterType.CODE; 119 0 : break; 120 : case PERMISSIONS: 121 1 : type = FilterType.PERMISSIONS; 122 1 : break; 123 : default: 124 0 : throw new BadRequestException("Unknown filter type: " + request.getFilterType()); 125 : } 126 3 : lp.setFilterType(type); 127 : 128 3 : lp.setAll(request.isAll()); 129 : 130 3 : lp.setState(request.getState()); 131 : 132 3 : return lp.apply(); 133 : } 134 : 135 : @Override 136 : public QueryRequest query() { 137 3 : return new QueryRequest() { 138 : @Override 139 : public List<ProjectInfo> get() throws RestApiException { 140 3 : return ProjectsImpl.this.query(this); 141 : } 142 : }; 143 : } 144 : 145 : @Override 146 : public QueryRequest query(String query) { 147 3 : return query().withQuery(query); 148 : } 149 : 150 : private List<ProjectInfo> query(QueryRequest r) throws RestApiException { 151 : try { 152 3 : return queryProvider 153 3 : .get() 154 3 : .withQuery(r.getQuery()) 155 3 : .withLimit(r.getLimit()) 156 3 : .withStart(r.getStart()) 157 3 : .apply(); 158 0 : } catch (StorageException e) { 159 0 : throw asRestApiException("Cannot query projects", e); 160 : } 161 : } 162 : }