Line data Source code
1 : // Copyright (C) 2014 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.git.validators; 16 : 17 : import com.google.gerrit.entities.Project; 18 : import com.google.gerrit.server.plugincontext.PluginSetContext; 19 : import com.google.gerrit.server.validators.ValidationException; 20 : import com.google.inject.Inject; 21 : import com.google.inject.assistedinject.Assisted; 22 : import java.util.Collection; 23 : import org.eclipse.jgit.lib.ObjectId; 24 : import org.eclipse.jgit.lib.Repository; 25 : import org.eclipse.jgit.transport.PreUploadHook; 26 : import org.eclipse.jgit.transport.ServiceMayNotContinueException; 27 : import org.eclipse.jgit.transport.UploadPack; 28 : 29 : /** Collection of validators to run before Gerrit sends pack data to a client. */ 30 : public class UploadValidators implements PreUploadHook { 31 : 32 : private final PluginSetContext<UploadValidationListener> uploadValidationListeners; 33 : private final Project project; 34 : private final Repository repository; 35 : private final String remoteHost; 36 : 37 : public interface Factory { 38 : UploadValidators create(Project project, Repository repository, String remoteAddress); 39 : } 40 : 41 : @Inject 42 : UploadValidators( 43 : PluginSetContext<UploadValidationListener> uploadValidationListeners, 44 : @Assisted Project project, 45 : @Assisted Repository repository, 46 134 : @Assisted String remoteHost) { 47 134 : this.uploadValidationListeners = uploadValidationListeners; 48 134 : this.project = project; 49 134 : this.repository = repository; 50 134 : this.remoteHost = remoteHost; 51 134 : } 52 : 53 : @Override 54 : public void onSendPack( 55 : UploadPack up, Collection<? extends ObjectId> wants, Collection<? extends ObjectId> haves) 56 : throws ServiceMayNotContinueException { 57 : try { 58 134 : uploadValidationListeners.runEach( 59 0 : l -> l.onPreUpload(repository, project, remoteHost, up, wants, haves), 60 : ValidationException.class); 61 0 : } catch (ValidationException e) { 62 0 : throw new UploadValidationException(e.getMessage()); 63 134 : } 64 134 : } 65 : 66 : @Override 67 : public void onBeginNegotiateRound( 68 : UploadPack up, Collection<? extends ObjectId> wants, int cntOffered) 69 : throws ServiceMayNotContinueException { 70 : try { 71 134 : uploadValidationListeners.runEach( 72 0 : l -> l.onBeginNegotiate(repository, project, remoteHost, up, wants, cntOffered), 73 : ValidationException.class); 74 0 : } catch (ValidationException e) { 75 0 : throw new UploadValidationException(e.getMessage()); 76 134 : } 77 134 : } 78 : 79 : @Override 80 : public void onEndNegotiateRound( 81 : UploadPack up, 82 : Collection<? extends ObjectId> wants, 83 : int cntCommon, 84 : int cntNotFound, 85 : boolean ready) 86 37 : throws ServiceMayNotContinueException {} 87 : }