├── .gitignore ├── header.txt ├── virusscan ├── README.md ├── src │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── sonatype │ │ │ └── nexus │ │ │ └── examples │ │ │ └── virusscan │ │ │ ├── VirusScanner.java │ │ │ ├── ExampleVirusScanner.java │ │ │ ├── InfectedItemFoundEvent.java │ │ │ ├── VirusScannerRepositoryCustomizer.java │ │ │ └── VirusScannerRequestProcessor.java │ └── test │ │ └── java │ │ └── org │ │ └── sonatype │ │ └── nexus │ │ └── examples │ │ └── virusscan │ │ ├── ExampleVirusScannerTest.java │ │ └── VirusScannerRequestProcessorTest.java └── pom.xml ├── pro-plugins ├── README.md ├── stagingrules │ ├── README.md │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── org │ │ │ └── sonatype │ │ │ └── nexus │ │ │ └── examples │ │ │ └── stagingrules │ │ │ ├── Maven220BlockingStagingRuleType.java │ │ │ ├── BrokenArtifactStagingRuleType.java │ │ │ ├── Maven220BlockingStagingRuleEvaluator.java │ │ │ └── BrokenArtifactStagingRuleEvaluator.java │ └── pom.xml └── pom.xml ├── crawling ├── README.md ├── src │ └── main │ │ └── java │ │ └── org │ │ └── sonatype │ │ └── nexus │ │ └── examples │ │ └── crawling │ │ ├── GavCollector.java │ │ ├── ArtifactDiscoveryListener.java │ │ └── internal │ │ ├── task │ │ ├── CrawlTaskDescriptor.java │ │ └── CrawlTask.java │ │ ├── GavCollectorImpl.java │ │ ├── CollectGavsWalkerProcessor.java │ │ └── FileArtifactDiscoveryListener.java └── pom.xml ├── selectionactors ├── src │ └── main │ │ └── java │ │ └── org │ │ └── sonatype │ │ └── nexus │ │ └── examples │ │ └── selectionactors │ │ ├── SelectionFactory.java │ │ ├── Selection.java │ │ ├── SelectionEntry.java │ │ ├── Actor.java │ │ ├── SelectionCollector.java │ │ ├── Selector.java │ │ ├── inmemory │ │ ├── InMemorySelectionFactory.java │ │ ├── SimpleSelectionEntry.java │ │ ├── InMemorySelection.java │ │ └── InMemorySelectionCollector.java │ │ ├── selectors │ │ ├── AbstractSelector.java │ │ ├── TargetWalkerProcessor.java │ │ ├── GAVWalkerProcessor.java │ │ ├── TagWalkerProcessor.java │ │ ├── AbstractWalkingSelector.java │ │ ├── GAVSelector.java │ │ ├── GAVPC.java │ │ ├── TargetSelector.java │ │ └── TagSelector.java │ │ ├── model │ │ └── RunReportDTO.java │ │ ├── actors │ │ ├── DeleteActor.java │ │ └── TagActor.java │ │ └── rest │ │ └── SelectorActorResource.java ├── pom.xml └── README.md ├── README.md ├── attributes ├── src │ ├── test │ │ ├── java │ │ │ └── org │ │ │ │ └── sonatype │ │ │ │ └── nexus │ │ │ │ └── examples │ │ │ │ └── attributes │ │ │ │ ├── it │ │ │ │ ├── SanityIT.java │ │ │ │ └── AttributesITSupport.java │ │ │ │ └── rest │ │ │ │ └── ItemAttributesResourceTest.java │ │ └── filtered-resources │ │ │ └── logback-test.xml │ └── main │ │ └── java │ │ └── org │ │ └── sonatype │ │ └── nexus │ │ └── examples │ │ └── attributes │ │ ├── model │ │ ├── AttributesDTO.java │ │ └── AttributeDTO.java │ │ └── rest │ │ └── ItemAttributesResource.java ├── pom.xml └── README.md ├── url-realm-nexus-plugin ├── src │ ├── main │ │ └── java │ │ │ └── org │ │ │ └── sonatype │ │ │ └── nexus │ │ │ └── examples │ │ │ └── url │ │ │ ├── UrlRealmPlugin.java │ │ │ └── UrlRealm.java │ └── test │ │ └── java │ │ └── org │ │ └── sonatype │ │ └── nexus │ │ └── examples │ │ └── url │ │ └── UrlRealmTest.java └── pom.xml └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .project 3 | .classpath 4 | .settings 5 | .idea/ 6 | *.iml -------------------------------------------------------------------------------- /header.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 2 | 3 | This program is licensed to you under the Apache License Version 2.0, 4 | and you may not use this file except in compliance with the Apache License Version 2.0. 5 | You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 6 | 7 | Unless required by applicable law or agreed to in writing, 8 | software distributed under the Apache License Version 2.0 is distributed on an 9 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 11 | -------------------------------------------------------------------------------- /virusscan/README.md: -------------------------------------------------------------------------------- 1 | 15 | # Nexus VirusScan Example Plugin 16 | 17 | TODO -------------------------------------------------------------------------------- /pro-plugins/README.md: -------------------------------------------------------------------------------- 1 | 15 | # Nexus Professional Example Plugins 16 | 17 | TODO -------------------------------------------------------------------------------- /crawling/README.md: -------------------------------------------------------------------------------- 1 | 15 | # Nexus Repository Crawling Example Plugin 16 | 17 | TODO -------------------------------------------------------------------------------- /pro-plugins/stagingrules/README.md: -------------------------------------------------------------------------------- 1 | 15 | # Nexus Staging-Rules Example Plugin 16 | 17 | TODO -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/SelectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors; 14 | 15 | /** 16 | * ??? 17 | * 18 | * @since 1.0 19 | */ 20 | public interface SelectionFactory 21 | { 22 | SelectionCollector getCollector(); 23 | } 24 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/Selection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors; 14 | 15 | /** 16 | * ??? 17 | * 18 | * @since 1.0 19 | */ 20 | public interface Selection 21 | extends Iterable 22 | { 23 | int size(); 24 | 25 | void close(); 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 15 | # Nexus Example Plugins 16 | 17 | Some examples of Nexus plugins. 18 | 19 | ## Building 20 | 21 | ### Requirements 22 | 23 | * Apache Maven 3.0.4 or 3.0.5 24 | * Java 7 25 | 26 | Note: The master branch builds plugins that work with the development version of Nexus. For production systems check out the branch that corresponds to your version (e.g. 2.3.x) 27 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/SelectionEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors; 14 | 15 | import org.sonatype.nexus.proxy.repository.Repository; 16 | 17 | /** 18 | * ??? 19 | * 20 | * @since 1.0 21 | */ 22 | public interface SelectionEntry 23 | { 24 | Repository getRepository(); 25 | 26 | String getPath(); 27 | } 28 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/Actor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors; 14 | 15 | import java.io.IOException; 16 | import java.util.Map; 17 | 18 | /** 19 | * ??? 20 | * 21 | * @since 1.0 22 | */ 23 | public interface Actor 24 | { 25 | int perform(Selection selection, Map terms) 26 | throws IOException; 27 | } 28 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/SelectionCollector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors; 14 | 15 | import org.sonatype.nexus.proxy.item.StorageItem; 16 | 17 | /** 18 | * ??? 19 | * 20 | * @since 1.0 21 | */ 22 | public interface SelectionCollector 23 | { 24 | void add(StorageItem item); 25 | 26 | void remove(StorageItem item); 27 | 28 | Selection done(); 29 | } 30 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/Selector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors; 14 | 15 | import java.util.Map; 16 | 17 | import org.sonatype.nexus.proxy.repository.Repository; 18 | 19 | /** 20 | * ??? 21 | * 22 | * @since 1.0 23 | */ 24 | public interface Selector 25 | { 26 | /** 27 | * Performs a selection against given repository with given terms. 28 | */ 29 | Selection select(Repository repository, Map terms); 30 | } 31 | -------------------------------------------------------------------------------- /virusscan/src/main/java/org/sonatype/nexus/examples/virusscan/VirusScanner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.virusscan; 14 | 15 | import org.sonatype.nexus.proxy.item.StorageFileItem; 16 | 17 | /** 18 | * Virus scanner pluggable abstraction. 19 | * 20 | * @since 1.0 21 | */ 22 | public interface VirusScanner 23 | { 24 | /** 25 | * Check if the given item has a virus or not. 26 | * 27 | * @return True if the item has a virus; else false. 28 | */ 29 | boolean hasVirus(StorageFileItem item); 30 | } 31 | -------------------------------------------------------------------------------- /crawling/src/main/java/org/sonatype/nexus/examples/crawling/GavCollector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.crawling; 14 | 15 | import java.io.IOException; 16 | 17 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 18 | import org.sonatype.nexus.proxy.maven.MavenRepository; 19 | 20 | /** 21 | * ??? 22 | * 23 | * @since 1.0 24 | */ 25 | public interface GavCollector 26 | { 27 | void collectGAVs(ResourceStoreRequest request, MavenRepository mavenRepository, ArtifactDiscoveryListener listener) 28 | throws IOException; 29 | } 30 | -------------------------------------------------------------------------------- /attributes/src/test/java/org/sonatype/nexus/examples/attributes/it/SanityIT.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.attributes.it; 14 | 15 | import org.junit.Test; 16 | 17 | /** 18 | * Ensures basic plugin sanity. 19 | */ 20 | public class SanityIT 21 | extends AttributesITSupport 22 | { 23 | public SanityIT(final String nexusBundleCoordinates) { 24 | super(nexusBundleCoordinates); 25 | } 26 | 27 | @Test 28 | public void pluginIsLoaded() throws Exception { 29 | // TODO: Check that the plugin is actually loaded 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /attributes/src/test/filtered-resources/logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 17 | 18 | 19 | System.out 20 | 21 | %date %level [%thread%X{DC}] %logger - %msg%n 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /crawling/src/main/java/org/sonatype/nexus/examples/crawling/ArtifactDiscoveryListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.crawling; 14 | 15 | import org.sonatype.nexus.proxy.item.StorageItem; 16 | import org.sonatype.nexus.proxy.maven.MavenRepository; 17 | import org.sonatype.nexus.proxy.maven.gav.Gav; 18 | 19 | /** 20 | * ??? 21 | * 22 | * @since 1.0 23 | */ 24 | public interface ArtifactDiscoveryListener 25 | { 26 | void beforeWalk(final MavenRepository mavenRepository); 27 | 28 | void onArtifactDiscovery(final MavenRepository mavenRepository, final Gav gav, final StorageItem item); 29 | 30 | void afterWalk(final MavenRepository mavenRepository); 31 | } 32 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/inmemory/InMemorySelectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.inmemory; 14 | 15 | import javax.inject.Named; 16 | import javax.inject.Singleton; 17 | 18 | import org.sonatype.nexus.examples.selectionactors.SelectionCollector; 19 | import org.sonatype.nexus.examples.selectionactors.SelectionFactory; 20 | 21 | /** 22 | * ??? 23 | * 24 | * @since 1.0 25 | */ 26 | @Named 27 | @Singleton 28 | public class InMemorySelectionFactory 29 | implements SelectionFactory 30 | { 31 | @Override 32 | public SelectionCollector getCollector() { 33 | return new InMemorySelectionCollector(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /virusscan/src/main/java/org/sonatype/nexus/examples/virusscan/ExampleVirusScanner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.virusscan; 14 | 15 | import javax.inject.Named; 16 | import javax.inject.Singleton; 17 | 18 | import org.sonatype.nexus.proxy.item.StorageFileItem; 19 | import org.sonatype.sisu.goodies.common.ComponentSupport; 20 | 21 | /** 22 | * Example {@link VirusScanner} implementation which simulates an infection for any item name containing the string 23 | * "infected". 24 | * 25 | * @since 1.0 26 | */ 27 | @Named 28 | @Singleton 29 | public class ExampleVirusScanner 30 | extends ComponentSupport 31 | implements VirusScanner 32 | { 33 | public boolean hasVirus(final StorageFileItem item) { 34 | return item.getName().contains("infected"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /pro-plugins/stagingrules/src/main/java/org/sonatype/nexus/examples/stagingrules/Maven220BlockingStagingRuleType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.stagingrules; 14 | 15 | import javax.inject.Named; 16 | import javax.inject.Singleton; 17 | 18 | import com.sonatype.nexus.staging.rule.RuleType; 19 | 20 | /** 21 | * {@link Maven220BlockingStagingRuleEvaluator} UI descriptor. 22 | * 23 | * @since 1.0 24 | */ 25 | @Named(Maven220BlockingStagingRuleType.TYPE_ID) 26 | @Singleton 27 | public class Maven220BlockingStagingRuleType 28 | implements RuleType 29 | { 30 | public static final String TYPE_ID = "maven220blocking-staging"; 31 | 32 | public String getDescription() { 33 | return "Verifies Maven 2.2.0 was not used to stage a repository."; 34 | } 35 | 36 | public String getId() { 37 | return TYPE_ID; 38 | } 39 | 40 | public String getName() { 41 | return "Staging Maven 2.2.0 Blocker"; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /pro-plugins/stagingrules/src/main/java/org/sonatype/nexus/examples/stagingrules/BrokenArtifactStagingRuleType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.stagingrules; 14 | 15 | import javax.inject.Singleton; 16 | import javax.inject.Named; 17 | 18 | import com.sonatype.nexus.staging.rule.RuleType; 19 | 20 | /** 21 | * {@link BrokenArtifactStagingRuleEvaluator} UI descriptor. 22 | * 23 | * @since 1.0 24 | */ 25 | @Named(BrokenArtifactStagingRuleType.TYPE_ID) 26 | @Singleton 27 | public class BrokenArtifactStagingRuleType 28 | implements RuleType 29 | { 30 | public static final String TYPE_ID = "brokenartifact-staging"; 31 | 32 | public String getDescription() { 33 | return "Verifies the string 'broken' is not contained in an artifacts path."; 34 | } 35 | 36 | public String getId() { 37 | return TYPE_ID; 38 | } 39 | 40 | public String getName() { 41 | return "Staging Broken Artifact"; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/inmemory/SimpleSelectionEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.inmemory; 14 | 15 | import org.sonatype.nexus.examples.selectionactors.SelectionEntry; 16 | import org.sonatype.nexus.proxy.item.RepositoryItemUid; 17 | import org.sonatype.nexus.proxy.repository.Repository; 18 | 19 | import com.google.common.base.Preconditions; 20 | 21 | /** 22 | * ??? 23 | * 24 | * @since 1.0 25 | */ 26 | public class SimpleSelectionEntry 27 | implements SelectionEntry 28 | { 29 | private final RepositoryItemUid uid; 30 | 31 | public SimpleSelectionEntry(final RepositoryItemUid uid) { 32 | this.uid = Preconditions.checkNotNull(uid); 33 | } 34 | 35 | @Override 36 | public Repository getRepository() { 37 | return uid.getRepository(); 38 | } 39 | 40 | @Override 41 | public String getPath() { 42 | return uid.getPath(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /attributes/src/main/java/org/sonatype/nexus/examples/attributes/model/AttributesDTO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.attributes.model; 14 | 15 | import java.util.Iterator; 16 | import java.util.List; 17 | 18 | import com.google.common.collect.Lists; 19 | import com.thoughtworks.xstream.annotations.XStreamAlias; 20 | 21 | /** 22 | * Attribute collection. 23 | * 24 | * @since 1.0 25 | */ 26 | @XStreamAlias("attributes") 27 | public class AttributesDTO 28 | implements Iterable 29 | { 30 | private final List attributes = Lists.newArrayList(); 31 | 32 | public List getAttributes() { 33 | return attributes; 34 | } 35 | 36 | @Override 37 | public Iterator iterator() { 38 | return attributes.iterator(); 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return getClass().getSimpleName() + "{" + 44 | "attributes=" + attributes + 45 | '}'; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/AbstractSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import javax.inject.Inject; 16 | 17 | import org.sonatype.nexus.examples.selectionactors.SelectionFactory; 18 | import org.sonatype.nexus.examples.selectionactors.Selector; 19 | import org.sonatype.sisu.goodies.common.ComponentSupport; 20 | 21 | import com.google.common.base.Preconditions; 22 | 23 | /** 24 | * ??? 25 | * 26 | * @since 1.0 27 | */ 28 | public abstract class AbstractSelector 29 | extends ComponentSupport 30 | implements Selector 31 | { 32 | private final SelectionFactory selectionFactory; 33 | 34 | @Inject 35 | public AbstractSelector(final SelectionFactory selectionFactory) { 36 | this.selectionFactory = Preconditions.checkNotNull(selectionFactory); 37 | } 38 | 39 | protected SelectionFactory getSelectionFactory() { 40 | return selectionFactory; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /attributes/src/main/java/org/sonatype/nexus/examples/attributes/model/AttributeDTO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.attributes.model; 14 | 15 | import javax.annotation.Nullable; 16 | 17 | import com.thoughtworks.xstream.annotations.XStreamAlias; 18 | 19 | import static com.google.common.base.Preconditions.checkNotNull; 20 | 21 | /** 22 | * Attribute key-value pair. 23 | * 24 | * @since 1.0 25 | */ 26 | @XStreamAlias("attribute") 27 | public class AttributeDTO 28 | { 29 | private final String key; 30 | 31 | private final String value; 32 | 33 | public AttributeDTO(final String key, final @Nullable String value) { 34 | this.key = checkNotNull(key); 35 | this.value = value; 36 | } 37 | 38 | public String getKey() { 39 | return key; 40 | } 41 | 42 | public String getValue() { 43 | return value; 44 | } 45 | 46 | 47 | @Override 48 | public String toString() { 49 | return getClass().getSimpleName() + "{" + 50 | "key='" + key + '\'' + 51 | ", value='" + value + '\'' + 52 | '}'; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /url-realm-nexus-plugin/src/main/java/org/sonatype/nexus/examples/url/UrlRealmPlugin.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.url; 14 | 15 | import javax.inject.Inject; 16 | import javax.inject.Named; 17 | 18 | import org.sonatype.nexus.plugin.PluginIdentity; 19 | 20 | import org.eclipse.sisu.EagerSingleton; 21 | import org.jetbrains.annotations.NonNls; 22 | 23 | /** 24 | * URL Realm plugin. 25 | */ 26 | @Named 27 | @EagerSingleton 28 | public class UrlRealmPlugin 29 | extends PluginIdentity 30 | { 31 | /** 32 | * Prefix for ID-like things. 33 | */ 34 | @NonNls 35 | public static final String ID_PREFIX = "url-realm"; 36 | 37 | /** 38 | * Expected groupId for plugin artifact. 39 | */ 40 | @NonNls 41 | public static final String GROUP_ID = "org.sonatype.nexus.examples"; 42 | 43 | /** 44 | * Expected artifactId for plugin artifact. 45 | */ 46 | @NonNls 47 | public static final String ARTIFACT_ID = ID_PREFIX + "-nexus-plugin"; 48 | 49 | @Inject 50 | public UrlRealmPlugin() throws Exception { 51 | super(GROUP_ID, ARTIFACT_ID); 52 | } 53 | } -------------------------------------------------------------------------------- /crawling/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.sonatype.nexus.examples 23 | nexus-examples 24 | 1.0-SNAPSHOT 25 | 26 | 27 | crawling-nexus-plugin 28 | ${project.groupId}:${project.artifactId} 29 | nexus-plugin 30 | 31 | 32 | 33 | org.sonatype.nexus 34 | nexus-plugin-api 35 | provided 36 | 37 | 38 | 39 | org.sonatype.nexus 40 | nexus-plugin-testsupport 41 | test 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /virusscan/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.sonatype.nexus.examples 23 | nexus-examples 24 | 1.0-SNAPSHOT 25 | 26 | 27 | virusscan-nexus-plugin 28 | ${project.groupId}:${project.artifactId} 29 | nexus-plugin 30 | 31 | 32 | 33 | org.sonatype.nexus 34 | nexus-plugin-api 35 | provided 36 | 37 | 38 | 39 | org.sonatype.nexus 40 | nexus-plugin-testsupport 41 | test 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /virusscan/src/main/java/org/sonatype/nexus/examples/virusscan/InfectedItemFoundEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.virusscan; 14 | 15 | import org.sonatype.nexus.events.AbstractEvent; 16 | import org.sonatype.nexus.proxy.item.StorageFileItem; 17 | import org.sonatype.nexus.proxy.repository.Repository; 18 | 19 | import static com.google.common.base.Preconditions.checkNotNull; 20 | 21 | /** 22 | * Event fired when a virus has been detected in an item. 23 | * 24 | * @since 1.0 25 | */ 26 | public class InfectedItemFoundEvent 27 | extends AbstractEvent 28 | { 29 | private final StorageFileItem item; 30 | 31 | public InfectedItemFoundEvent(final Repository repository, final StorageFileItem item) { 32 | super(repository); 33 | this.item = checkNotNull(item); 34 | } 35 | 36 | public Repository getRepository() { 37 | return getEventSender(); 38 | } 39 | 40 | public StorageFileItem getItem() { 41 | return item; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return getClass().getSimpleName() + "{" + 47 | "item=" + item + 48 | '}'; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/inmemory/InMemorySelection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.inmemory; 14 | 15 | import java.util.ArrayList; 16 | import java.util.Collection; 17 | import java.util.Iterator; 18 | 19 | import org.sonatype.nexus.examples.selectionactors.Selection; 20 | import org.sonatype.nexus.examples.selectionactors.SelectionEntry; 21 | import org.sonatype.nexus.proxy.item.RepositoryItemUid; 22 | 23 | /** 24 | * ??? 25 | * 26 | * @since 1.0 27 | */ 28 | public class InMemorySelection 29 | implements Selection 30 | { 31 | private final ArrayList entries; 32 | 33 | public InMemorySelection(final Collection entries) { 34 | this.entries = new ArrayList(entries.size()); 35 | for (RepositoryItemUid uid : entries) { 36 | this.entries.add(new SimpleSelectionEntry(uid)); 37 | } 38 | } 39 | 40 | @Override 41 | public Iterator iterator() { 42 | return entries.iterator(); 43 | } 44 | 45 | @Override 46 | public int size() { 47 | return entries.size(); 48 | } 49 | 50 | @Override 51 | public void close() { 52 | // nothing particular 53 | entries.clear(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/TargetWalkerProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import org.sonatype.nexus.examples.selectionactors.SelectionCollector; 16 | import org.sonatype.nexus.proxy.item.StorageItem; 17 | import org.sonatype.nexus.proxy.targets.Target; 18 | import org.sonatype.nexus.proxy.walker.AbstractWalkerProcessor; 19 | import org.sonatype.nexus.proxy.walker.WalkerContext; 20 | 21 | /** 22 | * ??? 23 | * 24 | * @since 1.0 25 | */ 26 | public class TargetWalkerProcessor 27 | extends AbstractWalkerProcessor 28 | { 29 | private final SelectionCollector selectionCollector; 30 | 31 | private final Target target; 32 | 33 | public TargetWalkerProcessor(final SelectionCollector selectionCollector, final Target target) { 34 | this.selectionCollector = selectionCollector; 35 | this.target = target; 36 | } 37 | 38 | @Override 39 | public void processItem(final WalkerContext context, final StorageItem item) 40 | throws Exception 41 | { 42 | if (target.isPathContained(context.getRepository().getRepositoryContentClass(), 43 | item.getRepositoryItemUid().getPath())) { 44 | selectionCollector.add(item); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /virusscan/src/test/java/org/sonatype/nexus/examples/virusscan/ExampleVirusScannerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.virusscan; 14 | 15 | import org.sonatype.nexus.proxy.item.StorageFileItem; 16 | import org.sonatype.sisu.litmus.testsupport.TestSupport; 17 | 18 | import org.junit.Before; 19 | import org.junit.Test; 20 | 21 | import static org.hamcrest.Matchers.is; 22 | import static org.junit.Assert.assertThat; 23 | import static org.mockito.Mockito.mock; 24 | import static org.mockito.Mockito.when; 25 | 26 | /** 27 | * Tests for {@link ExampleVirusScanner}. 28 | */ 29 | public class ExampleVirusScannerTest 30 | extends TestSupport 31 | { 32 | private ExampleVirusScanner underTest; 33 | 34 | @Before 35 | public void setUp() throws Exception { 36 | underTest = new ExampleVirusScanner(); 37 | } 38 | 39 | @Test 40 | public void nonInfectedItem() throws Exception { 41 | StorageFileItem item = mock(StorageFileItem.class); 42 | when(item.getName()).thenReturn("some-clean-item"); 43 | assertThat(underTest.hasVirus(item), is(false)); 44 | } 45 | 46 | @Test 47 | public void infectedItem() throws Exception { 48 | StorageFileItem item = mock(StorageFileItem.class); 49 | when(item.getName()).thenReturn("some-infected-item"); 50 | assertThat(underTest.hasVirus(item), is(true)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /pro-plugins/stagingrules/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.sonatype.nexus.examples 23 | pro-plugins 24 | 1.0-SNAPSHOT 25 | 26 | 27 | stagingrules-nexus-plugin 28 | ${project.groupId}:${project.artifactId} 29 | nexus-plugin 30 | 31 | 32 | 33 | org.sonatype.nexus 34 | nexus-plugin-api 35 | provided 36 | 37 | 38 | 39 | com.sonatype.nexus.plugins 40 | nexus-staging-plugin 41 | ${nexus-plugin.type} 42 | provided 43 | 44 | 45 | 46 | org.sonatype.nexus 47 | nexus-plugin-testsupport 48 | test 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /selectionactors/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.sonatype.nexus.examples 23 | nexus-examples 24 | 1.0-SNAPSHOT 25 | 26 | 27 | selectionactors-nexus-plugin 28 | ${project.groupId}:${project.artifactId} 29 | nexus-plugin 30 | 31 | 32 | 33 | org.sonatype.nexus 34 | nexus-plugin-api 35 | provided 36 | 37 | 38 | 39 | org.sonatype.nexus.plugins 40 | nexus-restlet1x-plugin 41 | ${nexus-plugin.type} 42 | provided 43 | 44 | 45 | 46 | org.sonatype.nexus 47 | nexus-plugin-testsupport 48 | test 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/inmemory/InMemorySelectionCollector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.inmemory; 14 | 15 | import java.util.ArrayList; 16 | 17 | import org.sonatype.nexus.examples.selectionactors.Selection; 18 | import org.sonatype.nexus.examples.selectionactors.SelectionCollector; 19 | import org.sonatype.nexus.proxy.item.RepositoryItemUid; 20 | import org.sonatype.nexus.proxy.item.StorageItem; 21 | 22 | /** 23 | * ??? 24 | * 25 | * @since 1.0 26 | */ 27 | public class InMemorySelectionCollector 28 | implements SelectionCollector 29 | { 30 | private final ArrayList selectedUids; 31 | 32 | public InMemorySelectionCollector() { 33 | this.selectedUids = new ArrayList(); 34 | } 35 | 36 | @Override 37 | public void add(final StorageItem item) { 38 | // duplicate? order? etc 39 | final RepositoryItemUid uid = item.getRepositoryItemUid(); 40 | selectedUids.add(uid); 41 | } 42 | 43 | @Override 44 | public void remove(final StorageItem item) { 45 | final RepositoryItemUid uid = item.getRepositoryItemUid(); 46 | selectedUids.remove(uid); 47 | } 48 | 49 | @Override 50 | public Selection done() { 51 | try { 52 | return new InMemorySelection(selectedUids); 53 | } 54 | finally { 55 | selectedUids.clear(); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /url-realm-nexus-plugin/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.sonatype.nexus.examples 23 | nexus-examples 24 | 1.0-SNAPSHOT 25 | 26 | 27 | nexus-url-realm-plugin 28 | ${project.groupId}:${project.artifactId} 29 | nexus-plugin 30 | 31 | 32 | Nexus URL Realm Plugin 33 | Adds URL Realm to Nexus. 34 | 35 | 36 | 37 | 38 | org.sonatype.nexus 39 | nexus-plugin-api 40 | provided 41 | 42 | 43 | 44 | org.sonatype.nexus 45 | nexus-plugin-testsupport 46 | test 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.sonatype.nexus 54 | nexus-plugin-bundle-maven-plugin 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /attributes/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.sonatype.nexus.examples 23 | nexus-examples 24 | 1.0-SNAPSHOT 25 | 26 | 27 | attributes-nexus-plugin 28 | ${project.groupId}:${project.artifactId} 29 | nexus-plugin 30 | 31 | 32 | 33 | org.sonatype.nexus 34 | nexus-plugin-api 35 | provided 36 | 37 | 38 | 39 | org.sonatype.nexus.plugins 40 | nexus-restlet1x-plugin 41 | ${nexus-plugin.type} 42 | provided 43 | 44 | 45 | 46 | org.sonatype.nexus 47 | nexus-plugin-testsupport 48 | test 49 | 50 | 51 | 52 | org.sonatype.nexus 53 | nexus-testsuite-support 54 | test 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /virusscan/src/main/java/org/sonatype/nexus/examples/virusscan/VirusScannerRepositoryCustomizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.virusscan; 14 | 15 | import javax.inject.Inject; 16 | import javax.inject.Named; 17 | 18 | import org.sonatype.configuration.ConfigurationException; 19 | import org.sonatype.nexus.plugins.RepositoryCustomizer; 20 | import org.sonatype.nexus.proxy.repository.ProxyRepository; 21 | import org.sonatype.nexus.proxy.repository.Repository; 22 | import org.sonatype.nexus.proxy.repository.RequestStrategy; 23 | 24 | import com.google.common.base.Preconditions; 25 | 26 | /** 27 | * Configures the {@link VirusScannerRepositoryCustomizer} on all proxy repositories. 28 | * 29 | * @since 1.0 30 | */ 31 | @Named(VirusScannerRequestProcessor.ID) 32 | public class VirusScannerRepositoryCustomizer 33 | implements RepositoryCustomizer 34 | { 35 | private final RequestStrategy processor; 36 | 37 | @Inject 38 | public VirusScannerRepositoryCustomizer(final @Named(VirusScannerRequestProcessor.ID) RequestStrategy processor) { 39 | this.processor = Preconditions.checkNotNull(processor); 40 | } 41 | 42 | @Override 43 | public boolean isHandledRepository(final Repository repository) { 44 | // handle proxy reposes only 45 | return repository.getRepositoryKind().isFacetAvailable(ProxyRepository.class); 46 | } 47 | 48 | @Override 49 | public void configureRepository(final Repository repository) throws ConfigurationException { 50 | repository.registerRequestStrategy(VirusScannerRequestProcessor.ID, processor); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/GAVWalkerProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import org.sonatype.nexus.examples.selectionactors.SelectionCollector; 16 | import org.sonatype.nexus.proxy.item.StorageItem; 17 | import org.sonatype.nexus.proxy.maven.MavenRepository; 18 | import org.sonatype.nexus.proxy.maven.gav.Gav; 19 | import org.sonatype.nexus.proxy.walker.AbstractWalkerProcessor; 20 | import org.sonatype.nexus.proxy.walker.WalkerContext; 21 | 22 | /** 23 | * ??? 24 | * 25 | * @since 1.0 26 | */ 27 | public class GAVWalkerProcessor 28 | extends AbstractWalkerProcessor 29 | { 30 | private final MavenRepository mavenRepository; 31 | 32 | private final GAVPC gavpc; 33 | 34 | private final SelectionCollector selectionCollector; 35 | 36 | public GAVWalkerProcessor(final MavenRepository mavenRepository, final GAVPC gavpc, 37 | final SelectionCollector selectionCollector) 38 | { 39 | this.mavenRepository = mavenRepository; 40 | this.gavpc = gavpc; 41 | this.selectionCollector = selectionCollector; 42 | } 43 | 44 | @Override 45 | public void processItem(final WalkerContext context, final StorageItem item) 46 | throws Exception 47 | { 48 | final Gav gav = mavenRepository.getGavCalculator().pathToGav(item.getRepositoryItemUid().getPath()); 49 | if (gav != null && gavpc.matches(gav)) { 50 | selectionCollector.add(item); 51 | } 52 | } 53 | 54 | public SelectionCollector getSelectionCollector() { 55 | return selectionCollector; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /attributes/src/test/java/org/sonatype/nexus/examples/attributes/rest/ItemAttributesResourceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.attributes.rest; 14 | 15 | import org.sonatype.nexus.examples.attributes.model.AttributeDTO; 16 | import org.sonatype.nexus.examples.attributes.model.AttributesDTO; 17 | import org.sonatype.nexus.proxy.attributes.Attributes; 18 | import org.sonatype.sisu.litmus.testsupport.TestSupport; 19 | 20 | import org.junit.Test; 21 | 22 | import static org.mockito.Mockito.mock; 23 | import static org.mockito.Mockito.verify; 24 | import static org.mockito.Mockito.verifyNoMoreInteractions; 25 | 26 | /** 27 | * Tests for {@link ItemAttributesResource}. 28 | */ 29 | public class ItemAttributesResourceTest 30 | extends TestSupport 31 | { 32 | @Test(expected = IllegalArgumentException.class) 33 | public void applyTo_canNotOverrideSystemAttribute() throws Exception { 34 | AttributesDTO attributes = new AttributesDTO(); 35 | attributes.getAttributes().add(new AttributeDTO(ItemAttributesResource.SYSTEM_ATTR_PREFIX + "foo", "bar")); 36 | Attributes target = mock(Attributes.class); 37 | 38 | ItemAttributesResource.applyTo(attributes, target); 39 | 40 | verifyNoMoreInteractions(target); 41 | } 42 | 43 | @Test 44 | public void applyTo_legalAttributeKey() throws Exception { 45 | AttributesDTO attributes = new AttributesDTO(); 46 | attributes.getAttributes().add(new AttributeDTO("foo", "bar")); 47 | Attributes target = mock(Attributes.class); 48 | 49 | ItemAttributesResource.applyTo(attributes, target); 50 | 51 | verify(target).put("foo", "bar"); 52 | verifyNoMoreInteractions(target); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/TagWalkerProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import org.apache.commons.lang.StringUtils; 16 | import org.sonatype.nexus.examples.selectionactors.SelectionCollector; 17 | import org.sonatype.nexus.proxy.item.StorageItem; 18 | import org.sonatype.nexus.proxy.walker.AbstractWalkerProcessor; 19 | import org.sonatype.nexus.proxy.walker.WalkerContext; 20 | 21 | /** 22 | * ??? 23 | * 24 | * @since 1.0 25 | */ 26 | public class TagWalkerProcessor 27 | extends AbstractWalkerProcessor 28 | { 29 | private final SelectionCollector selectionCollector; 30 | 31 | private final String attributeKey; 32 | 33 | private final String attributeValue; 34 | 35 | public TagWalkerProcessor(final SelectionCollector selectionCollector, final String attributeKey, 36 | final String attributeValue) 37 | { 38 | this.selectionCollector = selectionCollector; 39 | this.attributeKey = attributeKey; 40 | this.attributeValue = attributeValue; 41 | } 42 | 43 | @Override 44 | public void processItem(final WalkerContext context, final StorageItem item) 45 | throws Exception 46 | { 47 | // check for key existence 48 | if (item.getRepositoryItemAttributes().containsKey(attributeKey)) { 49 | // if value given, check for attribute value equality too 50 | if (attributeValue != null 51 | && !StringUtils.equals(attributeValue, item.getRepositoryItemAttributes().get(attributeKey))) { 52 | return; 53 | } 54 | selectionCollector.add(item); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/model/RunReportDTO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.model; 14 | 15 | import com.thoughtworks.xstream.annotations.XStreamAlias; 16 | 17 | /** 18 | * ??? 19 | * 20 | * @since 1.0 21 | */ 22 | @XStreamAlias("runReport") 23 | public class RunReportDTO 24 | { 25 | private final String repositoryId; 26 | 27 | private final String selectorId; 28 | 29 | private final int selectedCount; 30 | 31 | private final String actorId; 32 | 33 | private final int actedCount; 34 | 35 | private final boolean success; 36 | 37 | public RunReportDTO(final String repositoryId, final String selectorId, final int selectedCount, 38 | final String actorId, final int actedCount, final boolean success) 39 | { 40 | this.repositoryId = repositoryId; 41 | this.selectorId = selectorId; 42 | this.selectedCount = selectedCount; 43 | this.actorId = actorId; 44 | this.actedCount = actedCount; 45 | this.success = success; 46 | } 47 | 48 | protected String getRepositoryId() { 49 | return repositoryId; 50 | } 51 | 52 | protected String getSelectorId() { 53 | return selectorId; 54 | } 55 | 56 | protected int getSelectedCount() { 57 | return selectedCount; 58 | } 59 | 60 | protected String getActorId() { 61 | return actorId; 62 | } 63 | 64 | protected int getActedCount() { 65 | return actedCount; 66 | } 67 | 68 | protected boolean isSuccess() { 69 | return success; 70 | } 71 | 72 | @Override 73 | public String toString() { 74 | return getClass().getSimpleName() + "{" + 75 | "repositoryId='" + repositoryId + '\'' + 76 | ", selectorId='" + selectorId + '\'' + 77 | ", selectedCount=" + selectedCount + 78 | ", actorId='" + actorId + '\'' + 79 | ", actedCount=" + actedCount + 80 | ", success=" + success + 81 | '}'; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /crawling/src/main/java/org/sonatype/nexus/examples/crawling/internal/task/CrawlTaskDescriptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.crawling.internal.task; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | import javax.inject.Named; 19 | import javax.inject.Singleton; 20 | 21 | import org.sonatype.inject.Description; 22 | import org.sonatype.nexus.formfields.FormField; 23 | import org.sonatype.nexus.formfields.RepoOrGroupComboFormField; 24 | import org.sonatype.nexus.formfields.StringTextFormField; 25 | import org.sonatype.nexus.tasks.descriptors.AbstractScheduledTaskDescriptor; 26 | 27 | /** 28 | * {@link CrawlTask} UI descriptor. 29 | * 30 | * @since 1.0 31 | */ 32 | @Named 33 | @Singleton 34 | @Description("Crawling Task") 35 | public class CrawlTaskDescriptor 36 | extends AbstractScheduledTaskDescriptor 37 | { 38 | public static final String ID = "CrawlTask"; 39 | 40 | public static final String REPOSITORY_FIELD_ID = "repositoryId"; 41 | 42 | public static final String REPOSITORY_PATH_FIELD_ID = "repositoryPath"; 43 | 44 | private final RepoOrGroupComboFormField repoField = new RepoOrGroupComboFormField(REPOSITORY_FIELD_ID, 45 | RepoOrGroupComboFormField.DEFAULT_LABEL, "Type in the repository in which to run the task.", 46 | FormField.MANDATORY); 47 | 48 | private final StringTextFormField resourceStorePathField = new StringTextFormField(REPOSITORY_PATH_FIELD_ID, 49 | "Repository path", 50 | "Enter a repository path to run the task in recursively (ie. \"/\" for root or \"/org/apache\").", 51 | FormField.OPTIONAL); 52 | 53 | public String getId() { 54 | return ID; 55 | } 56 | 57 | public String getName() { 58 | return "Crawling Task"; 59 | } 60 | 61 | public List formFields() { 62 | List fields = new ArrayList(); 63 | 64 | fields.add(repoField); 65 | fields.add(resourceStorePathField); 66 | 67 | return fields; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/AbstractWalkingSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import javax.inject.Inject; 16 | 17 | import org.sonatype.nexus.examples.selectionactors.SelectionFactory; 18 | import org.sonatype.nexus.proxy.ItemNotFoundException; 19 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 20 | import org.sonatype.nexus.proxy.repository.Repository; 21 | import org.sonatype.nexus.proxy.walker.DefaultWalkerContext; 22 | import org.sonatype.nexus.proxy.walker.Walker; 23 | import org.sonatype.nexus.proxy.walker.WalkerException; 24 | import org.sonatype.nexus.proxy.walker.WalkerProcessor; 25 | 26 | import com.google.common.base.Preconditions; 27 | 28 | /** 29 | * ??? 30 | * 31 | * @since 1.0 32 | */ 33 | public abstract class AbstractWalkingSelector 34 | extends AbstractSelector 35 | { 36 | private Walker walker; 37 | 38 | @Inject 39 | public AbstractWalkingSelector(final SelectionFactory selectionFactory, final Walker walker) { 40 | super(Preconditions.checkNotNull(selectionFactory)); 41 | this.walker = Preconditions.checkNotNull(walker); 42 | } 43 | 44 | protected Walker getWalker() { 45 | return walker; 46 | } 47 | 48 | protected void walk(final Repository repository, final ResourceStoreRequest resourceStoreRequest, 49 | final WalkerProcessor walkerProcessor) 50 | throws WalkerException 51 | { 52 | final DefaultWalkerContext context = new DefaultWalkerContext(repository, resourceStoreRequest); 53 | context.getProcessors().add(walkerProcessor); 54 | try { 55 | walker.walk(context); 56 | } 57 | catch (WalkerException e) { 58 | if (!(e.getWalkerContext().getStopCause() instanceof ItemNotFoundException)) { 59 | // everything that is not ItemNotFound should be reported, 60 | // otherwise just neglect it 61 | throw e; 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/GAVSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import java.util.Map; 16 | 17 | import javax.inject.Inject; 18 | import javax.inject.Named; 19 | import javax.inject.Singleton; 20 | 21 | import org.sonatype.nexus.examples.selectionactors.Selection; 22 | import org.sonatype.nexus.examples.selectionactors.SelectionCollector; 23 | import org.sonatype.nexus.examples.selectionactors.SelectionFactory; 24 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 25 | import org.sonatype.nexus.proxy.maven.MavenRepository; 26 | import org.sonatype.nexus.proxy.repository.Repository; 27 | import org.sonatype.nexus.proxy.utils.RepositoryStringUtils; 28 | import org.sonatype.nexus.proxy.walker.Walker; 29 | 30 | import com.google.common.base.Preconditions; 31 | 32 | /** 33 | * ??? 34 | * 35 | * @since 1.0 36 | */ 37 | @Named("gav") 38 | @Singleton 39 | public class GAVSelector 40 | extends AbstractWalkingSelector 41 | { 42 | @Inject 43 | public GAVSelector(final SelectionFactory selectionFactory, final Walker walker) { 44 | super(Preconditions.checkNotNull(selectionFactory), Preconditions.checkNotNull(walker)); 45 | } 46 | 47 | @Override 48 | public Selection select(final Repository repository, final Map terms) { 49 | final MavenRepository mavenRepository = repository.adaptToFacet(MavenRepository.class); 50 | if (mavenRepository == null) { 51 | throw new IllegalArgumentException(RepositoryStringUtils.getFormattedMessage( 52 | "%s is not a maven repository!", repository)); 53 | } 54 | else { 55 | final SelectionCollector collector = getSelectionFactory().getCollector(); 56 | final GAVWalkerProcessor gwp = new GAVWalkerProcessor(mavenRepository, new GAVPC(terms), collector); 57 | walk(mavenRepository, new ResourceStoreRequest("/"), gwp); 58 | return collector.done(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /attributes/README.md: -------------------------------------------------------------------------------- 1 | 15 | # Nexus Attributes Example Plugin 16 | 17 | Example plugin for "Exposing attributes". 18 | 19 | Example session, adding "foo=bar" key-values to an item: 20 | 21 | ``` 22 | [cstamas@marvin tmp]$ curl -H "Accept:application/xml" http://localhost:8081/nexus/service/local/repositories/central/attributes/log4j/log4j/1.2.13/log4j-1.2.13.pom 23 | 24 | 25 | storageItem-remoteUrl 26 | http://repo1.maven.org/maven2/log4j/log4j/1.2.13/log4j-1.2.13.pom 27 | 28 | 29 | digest.sha1 30 | e5c244520e897865709c730433f8b0c44ef271f1 31 | 32 | … 33 | 34 | 35 | 36 | [cstamas@marvin tmp]$ curl -H "Accept:application/xml" -H "Content-Type:application/xml" -X PUT --data-binary "foobar" http://localhost:8081/nexus/service/local/repositories/central/attributes/log4j/log4j/1.2.13/log4j-1.2.13.pom 37 | 38 | 39 | 40 | storageItem-remoteUrl 41 | http://repo1.maven.org/maven2/log4j/log4j/1.2.13/log4j-1.2.13.pom 42 | 43 | … 44 | 45 | 46 | 47 | [cstamas@marvin tmp]$ curl -H "Accept:application/xml" http://localhost:8081/nexus/service/local/repositories/central/attributes/log4j/log4j/1.2.13/log4j-1.2.13.pom 48 | 49 | 50 | storageItem-remoteUrl 51 | http://repo1.maven.org/maven2/log4j/log4j/1.2.13/log4j-1.2.13.pom 52 | 53 | 54 | foo 55 | bar 56 | 57 | 58 | digest.sha1 59 | e5c244520e897865709c730433f8b0c44ef271f1 60 | 61 | … 62 | 63 | 64 | 65 | ``` -------------------------------------------------------------------------------- /pro-plugins/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.sonatype.nexus.examples 23 | nexus-examples 24 | 1.0-SNAPSHOT 25 | 26 | 27 | pro-plugins 28 | ${project.groupId}:${project.artifactId} 29 | pom 30 | 31 | 32 | com.sonatype.nexus 33 | nexus-professional-bundle-template 34 | ${nexus.version} 35 | 36 | 37 | 38 | stagingrules 39 | 40 | 41 | 42 | 43 | rso-private-nexus-dev 44 | https://repository.sonatype.org/content/groups/private-nexus-dev 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 56 | 57 | com.sonatype.nexus.plugins 58 | nexuspro-plugins 59 | pom 60 | ${nexus.version} 61 | import 62 | 63 | 64 | 65 | ${it.nexus.bundle.groupId} 66 | ${it.nexus.bundle.artifactId} 67 | zip 68 | bundle 69 | ${it.nexus.bundle.version} 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/GAVPC.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import java.util.Map; 16 | import java.util.regex.Pattern; 17 | 18 | import org.sonatype.nexus.proxy.maven.gav.Gav; 19 | 20 | /** 21 | * ??? 22 | * 23 | * @since 1.0 24 | */ 25 | public class GAVPC 26 | { 27 | private final Pattern g; 28 | 29 | private final Pattern a; 30 | 31 | private final Pattern v; 32 | 33 | private final Pattern bv; 34 | 35 | private final Pattern e; 36 | 37 | private final Pattern c; 38 | 39 | public GAVPC(final Map terms) { 40 | this.g = compileIfExists("g", terms); 41 | this.a = compileIfExists("a", terms); 42 | this.v = compileIfExists("v", terms); 43 | this.bv = compileIfExists("bv", terms); 44 | this.e = compileIfExists("e", terms); 45 | this.c = compileIfExists("c", terms); 46 | 47 | if (g == null && a == null && v == null && bv == null && e == null && c == null) { 48 | throw new IllegalArgumentException("No valid terms exists in map!"); 49 | } 50 | } 51 | 52 | protected Pattern compileIfExists(final String key, final Map terms) { 53 | if (terms.containsKey(key)) { 54 | return Pattern.compile(terms.get(key)); 55 | } 56 | else { 57 | return null; 58 | } 59 | } 60 | 61 | public boolean matches(final Gav gav) { 62 | if (g != null && !g.matcher(gav.getGroupId()).matches()) { 63 | return false; 64 | } 65 | if (a != null && !a.matcher(gav.getArtifactId()).matches()) { 66 | return false; 67 | } 68 | if (v != null && !v.matcher(gav.getVersion()).matches()) { 69 | return false; 70 | } 71 | if (bv != null && !bv.matcher(gav.getBaseVersion()).matches()) { 72 | return false; 73 | } 74 | if (e != null && !e.matcher(gav.getExtension()).matches()) { 75 | return false; 76 | } 77 | if (c != null && !c.matcher(gav.getClassifier()).matches()) { 78 | return false; 79 | } 80 | 81 | return true; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/actors/DeleteActor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.actors; 14 | 15 | import java.io.IOException; 16 | import java.util.Map; 17 | 18 | import javax.inject.Named; 19 | import javax.inject.Singleton; 20 | 21 | import org.sonatype.nexus.examples.selectionactors.Actor; 22 | import org.sonatype.nexus.examples.selectionactors.Selection; 23 | import org.sonatype.nexus.examples.selectionactors.SelectionEntry; 24 | import org.sonatype.nexus.proxy.IllegalOperationException; 25 | import org.sonatype.nexus.proxy.ItemNotFoundException; 26 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 27 | import org.sonatype.nexus.proxy.storage.UnsupportedStorageOperationException; 28 | 29 | /** 30 | * A simple "delete" actor, that deletes all the items in selection. 31 | * 32 | * @since 1.0 33 | */ 34 | @Named("delete") 35 | @Singleton 36 | public class DeleteActor 37 | implements Actor 38 | { 39 | @Override 40 | public int perform(final Selection selection, final Map terms) 41 | throws IOException 42 | { 43 | int acted = 0; 44 | try { 45 | for (SelectionEntry entry : selection) { 46 | try { 47 | doDelete(entry); 48 | acted++; 49 | } 50 | catch (ItemNotFoundException e) { 51 | // neglect and continue 52 | } 53 | } 54 | } 55 | catch (UnsupportedStorageOperationException e) { 56 | // repository storage does not allow? bail out 57 | } 58 | catch (IllegalOperationException e) { 59 | // repo does not allow this, is out of service or so? bail out 60 | } 61 | finally { 62 | selection.close(); 63 | } 64 | 65 | return acted; 66 | } 67 | 68 | // == 69 | 70 | protected void doDelete(final SelectionEntry entry) 71 | throws UnsupportedStorageOperationException, IllegalOperationException, ItemNotFoundException, IOException 72 | { 73 | final ResourceStoreRequest request = new ResourceStoreRequest(entry.getPath()); 74 | request.setRequestLocalOnly(true); 75 | entry.getRepository().deleteItem(false, request); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/TargetSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import java.util.Map; 16 | 17 | import javax.inject.Named; 18 | import javax.inject.Singleton; 19 | 20 | import org.sonatype.nexus.examples.selectionactors.Selection; 21 | import org.sonatype.nexus.examples.selectionactors.SelectionCollector; 22 | import org.sonatype.nexus.examples.selectionactors.SelectionFactory; 23 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 24 | import org.sonatype.nexus.proxy.repository.Repository; 25 | import org.sonatype.nexus.proxy.targets.Target; 26 | import org.sonatype.nexus.proxy.targets.TargetRegistry; 27 | import org.sonatype.nexus.proxy.walker.Walker; 28 | 29 | /** 30 | * ??? 31 | * 32 | * @since 1.0 33 | */ 34 | @Named("target") 35 | @Singleton 36 | public class TargetSelector 37 | extends AbstractWalkingSelector 38 | { 39 | /** 40 | * The key for target ID term. 41 | */ 42 | public static final String TERM_TARGET_ID = "targetId"; 43 | 44 | private final TargetRegistry targetRegistry; 45 | 46 | public TargetSelector(final SelectionFactory selectionFactory, final Walker walker, 47 | final TargetRegistry targetRegistry) { 48 | super(selectionFactory, walker); 49 | this.targetRegistry = targetRegistry; 50 | } 51 | 52 | @Override 53 | public Selection select(final Repository repository, final Map terms) { 54 | final String targetId = terms.get(TERM_TARGET_ID); 55 | if (targetId == null) { 56 | throw new IllegalArgumentException("Term " + TERM_TARGET_ID + " not found or is empty!"); 57 | } 58 | 59 | final Target target = targetRegistry.getRepositoryTarget(targetId); 60 | if (target == null) { 61 | throw new IllegalArgumentException("Target with ID=\"" + targetId + "\" not found!"); 62 | } 63 | 64 | final SelectionCollector collector = getSelectionFactory().getCollector(); 65 | final TargetWalkerProcessor twp = new TargetWalkerProcessor(collector, target); 66 | walk(repository, new ResourceStoreRequest("/"), twp); 67 | return collector.done(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /virusscan/src/test/java/org/sonatype/nexus/examples/virusscan/VirusScannerRequestProcessorTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.virusscan; 14 | 15 | import org.sonatype.nexus.proxy.item.StorageFileItem; 16 | import org.sonatype.sisu.goodies.eventbus.EventBus; 17 | import org.sonatype.sisu.litmus.testsupport.TestSupport; 18 | 19 | import com.google.common.collect.Lists; 20 | import org.junit.Before; 21 | import org.junit.Test; 22 | import org.mockito.Mock; 23 | 24 | import static org.hamcrest.Matchers.is; 25 | import static org.junit.Assert.assertThat; 26 | import static org.mockito.Matchers.any; 27 | import static org.mockito.Mockito.RETURNS_DEEP_STUBS; 28 | import static org.mockito.Mockito.mock; 29 | import static org.mockito.Mockito.verify; 30 | import static org.mockito.Mockito.when; 31 | 32 | /** 33 | * Tests for {@link VirusScannerRequestProcessor}. 34 | */ 35 | public class VirusScannerRequestProcessorTest 36 | extends TestSupport 37 | { 38 | private VirusScannerRequestProcessor underTest; 39 | 40 | @Mock 41 | private EventBus eventBus; 42 | 43 | @Mock 44 | private VirusScanner scanner1; 45 | 46 | @Mock 47 | private VirusScanner scanner2; 48 | 49 | @Before 50 | public void setUp() throws Exception { 51 | underTest = new VirusScannerRequestProcessor(eventBus, Lists.newArrayList(scanner1, scanner2)); 52 | } 53 | 54 | @Test 55 | public void allScannersConsulted() throws Exception { 56 | // first scanner does not detect 57 | when(scanner1.hasVirus(any(StorageFileItem.class))).thenReturn(false); 58 | 59 | // second scanner does 60 | when(scanner2.hasVirus(any(StorageFileItem.class))).thenReturn(true); 61 | 62 | // use a deep mock here, as the scanner needs to reach into the item to post and event 63 | StorageFileItem item = mock(StorageFileItem.class, RETURNS_DEEP_STUBS); 64 | 65 | // virus is detected (by one of the scanners) 66 | assertThat(underTest.hasVirus(item), is(true)); 67 | 68 | // both scanners are called 69 | verify(scanner1).hasVirus(item); 70 | verify(scanner2).hasVirus(item); 71 | 72 | // event is fired 73 | verify(eventBus).post(any(InfectedItemFoundEvent.class)); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/selectors/TagSelector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.selectors; 14 | 15 | import java.util.Map; 16 | 17 | import javax.inject.Inject; 18 | import javax.inject.Named; 19 | import javax.inject.Singleton; 20 | 21 | import org.sonatype.nexus.examples.selectionactors.Selection; 22 | import org.sonatype.nexus.examples.selectionactors.SelectionCollector; 23 | import org.sonatype.nexus.examples.selectionactors.SelectionFactory; 24 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 25 | import org.sonatype.nexus.proxy.repository.Repository; 26 | import org.sonatype.nexus.proxy.walker.Walker; 27 | 28 | import com.google.common.base.Preconditions; 29 | 30 | /** 31 | * ??? 32 | * 33 | * @since 1.0 34 | */ 35 | @Named("target") 36 | @Singleton 37 | public class TagSelector 38 | extends AbstractWalkingSelector 39 | { 40 | /** 41 | * The key for tag key term (mandatory). 42 | */ 43 | public static final String TERM_ATTRIBUTE_KEY = "attributeKey"; 44 | 45 | /** 46 | * The key for tag value term, optional. If given, will check for value equality, otherwise will check only for the 47 | * presence of {@link #TERM_ATTRIBUTE_KEY}. 48 | */ 49 | public static final String TERM_ATTRIBUTE_VALUE = "attributeValue"; 50 | 51 | @Inject 52 | public TagSelector(final SelectionFactory selectionFactory, final Walker walker) { 53 | super(Preconditions.checkNotNull(selectionFactory), Preconditions.checkNotNull(walker)); 54 | } 55 | 56 | 57 | @Override 58 | public Selection select(Repository repository, Map terms) { 59 | final String attributeKey = terms.get(TERM_ATTRIBUTE_KEY); 60 | if (attributeKey == null) { 61 | throw new IllegalArgumentException("Term " + TERM_ATTRIBUTE_KEY + " not found or is empty!"); 62 | } 63 | 64 | final String attributeValue = terms.get(TERM_ATTRIBUTE_VALUE); 65 | 66 | final SelectionCollector collector = getSelectionFactory().getCollector(); 67 | final TagWalkerProcessor twp = new TagWalkerProcessor(collector, attributeKey, attributeValue); 68 | walk(repository, new ResourceStoreRequest("/"), twp); 69 | return collector.done(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /attributes/src/test/java/org/sonatype/nexus/examples/attributes/it/AttributesITSupport.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.attributes.it; 14 | 15 | import java.util.Collection; 16 | 17 | import org.sonatype.nexus.bundle.launcher.NexusBundleConfiguration; 18 | import org.sonatype.nexus.testsuite.support.NexusRunningParametrizedITSupport; 19 | import org.sonatype.nexus.testsuite.support.NexusStartAndStopStrategy; 20 | 21 | import org.junit.runners.Parameterized; 22 | 23 | import static org.sonatype.nexus.testsuite.support.NexusStartAndStopStrategy.Strategy.EACH_TEST; 24 | import static org.sonatype.nexus.testsuite.support.ParametersLoaders.firstAvailableTestParameters; 25 | import static org.sonatype.nexus.testsuite.support.ParametersLoaders.systemTestParameters; 26 | import static org.sonatype.nexus.testsuite.support.ParametersLoaders.testParameters; 27 | import static org.sonatype.sisu.goodies.common.Varargs.$; 28 | 29 | /** 30 | * Support for attributes example plugin integration tests. 31 | */ 32 | @NexusStartAndStopStrategy(EACH_TEST) 33 | public abstract class AttributesITSupport 34 | extends NexusRunningParametrizedITSupport 35 | { 36 | @Parameterized.Parameters 37 | public static Collection data() { 38 | return firstAvailableTestParameters( 39 | systemTestParameters(), 40 | testParameters( 41 | $("${it.nexus.bundle.groupId}:${it.nexus.bundle.artifactId}:zip:bundle") 42 | ) 43 | ).load(); 44 | } 45 | 46 | public AttributesITSupport(final String nexusBundleCoordinates) { 47 | super(nexusBundleCoordinates); 48 | } 49 | 50 | @Override 51 | protected NexusBundleConfiguration configureNexus(final NexusBundleConfiguration configuration) { 52 | // override the format of the nexus.log file 53 | configuration.setLogPattern("%d{HH:mm:ss.SSS} %-5level - %msg%n"); 54 | 55 | // configure logging level of example plugins running in nexus 56 | configuration.setLogLevel("org.sonatype.nexus.examples", "DEBUG"); 57 | 58 | // install the plugin we are testing 59 | configuration.addPlugins( 60 | artifactResolver().resolvePluginFromDependencyManagement( 61 | "org.sonatype.nexus.examples", "attributes-nexus-plugin" 62 | ) 63 | ); 64 | 65 | return configuration; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /crawling/src/main/java/org/sonatype/nexus/examples/crawling/internal/GavCollectorImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.crawling.internal; 14 | 15 | import java.io.IOException; 16 | 17 | import javax.inject.Inject; 18 | import javax.inject.Named; 19 | import javax.inject.Singleton; 20 | 21 | import org.sonatype.nexus.examples.crawling.ArtifactDiscoveryListener; 22 | import org.sonatype.nexus.examples.crawling.GavCollector; 23 | import org.sonatype.nexus.proxy.ItemNotFoundException; 24 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 25 | import org.sonatype.nexus.proxy.item.RepositoryItemUid; 26 | import org.sonatype.nexus.proxy.maven.MavenRepository; 27 | import org.sonatype.nexus.proxy.walker.DefaultWalkerContext; 28 | import org.sonatype.nexus.proxy.walker.Walker; 29 | import org.sonatype.nexus.proxy.walker.WalkerContext; 30 | import org.sonatype.nexus.proxy.walker.WalkerException; 31 | import org.sonatype.sisu.goodies.common.ComponentSupport; 32 | 33 | import com.google.common.base.Preconditions; 34 | import org.apache.commons.lang.StringUtils; 35 | 36 | /** 37 | * ??? 38 | * 39 | * @since 1.0 40 | */ 41 | @Named 42 | @Singleton 43 | public class GavCollectorImpl 44 | extends ComponentSupport 45 | implements GavCollector 46 | { 47 | private final Walker walker; 48 | 49 | @Inject 50 | public GavCollectorImpl(final Walker walker) { 51 | this.walker = Preconditions.checkNotNull(walker); 52 | } 53 | 54 | @Override 55 | public void collectGAVs(final ResourceStoreRequest request, final MavenRepository mavenRepository, 56 | final ArtifactDiscoveryListener listener) 57 | throws IOException 58 | { 59 | if (StringUtils.isEmpty(request.getRequestPath())) { 60 | request.setRequestPath(RepositoryItemUid.PATH_ROOT); 61 | } 62 | // make sure we crawl local content (caches) only 63 | request.setRequestLocalOnly(true); 64 | final WalkerContext walkerContext = new DefaultWalkerContext(mavenRepository, request); 65 | final CollectGavsWalkerProcessor collectGavsWalkerProcessor = 66 | new CollectGavsWalkerProcessor(mavenRepository, listener); 67 | walkerContext.getProcessors().add(collectGavsWalkerProcessor); 68 | 69 | try { 70 | walker.walk(walkerContext); 71 | } 72 | catch (WalkerException e) { 73 | if (!(e.getWalkerContext().getStopCause() instanceof ItemNotFoundException)) { 74 | // everything that is not ItemNotFound should be reported, 75 | // otherwise just neglect it 76 | throw e; 77 | } 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /crawling/src/main/java/org/sonatype/nexus/examples/crawling/internal/CollectGavsWalkerProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.crawling.internal; 14 | 15 | import org.sonatype.nexus.examples.crawling.ArtifactDiscoveryListener; 16 | import org.sonatype.nexus.proxy.item.StorageCollectionItem; 17 | import org.sonatype.nexus.proxy.item.StorageItem; 18 | import org.sonatype.nexus.proxy.item.uid.IsHiddenAttribute; 19 | import org.sonatype.nexus.proxy.maven.MavenRepository; 20 | import org.sonatype.nexus.proxy.maven.gav.Gav; 21 | import org.sonatype.nexus.proxy.walker.AbstractWalkerProcessor; 22 | import org.sonatype.nexus.proxy.walker.WalkerContext; 23 | 24 | import com.google.common.base.Preconditions; 25 | 26 | /** 27 | * ??? 28 | * 29 | * @since 1.0 30 | */ 31 | public class CollectGavsWalkerProcessor 32 | extends AbstractWalkerProcessor 33 | { 34 | private final MavenRepository mavenRepository; 35 | 36 | private final ArtifactDiscoveryListener artifactDiscoveryListener; 37 | 38 | public CollectGavsWalkerProcessor(final MavenRepository mavenRepository, 39 | final ArtifactDiscoveryListener artifactDiscoveryListener) 40 | { 41 | this.mavenRepository = Preconditions.checkNotNull(mavenRepository); 42 | this.artifactDiscoveryListener = Preconditions.checkNotNull(artifactDiscoveryListener); 43 | } 44 | 45 | public MavenRepository getRepository() { 46 | return mavenRepository; 47 | } 48 | 49 | // == WalkerProcessor 50 | 51 | @Override 52 | public void beforeWalk(final WalkerContext context) 53 | throws Exception 54 | { 55 | super.beforeWalk(context); 56 | artifactDiscoveryListener.beforeWalk(getRepository()); 57 | } 58 | 59 | @Override 60 | public final void processItem(final WalkerContext context, final StorageItem item) 61 | throws Exception 62 | { 63 | if (item instanceof StorageCollectionItem) { 64 | return; // a directory 65 | } 66 | if (item.getRepositoryItemUid().getBooleanAttributeValue(IsHiddenAttribute.class)) { 67 | return; // leave out hidden stuff 68 | } 69 | 70 | // gav might be null, if item passed in does not obey maven2 layout 71 | // note: item still can be "file" or "link", but not "directory"! 72 | final Gav gav = getRepository().getGavCalculator().pathToGav(item.getPath()); 73 | artifactDiscoveryListener.onArtifactDiscovery(getRepository(), gav, item); 74 | } 75 | 76 | public void afterWalk(final WalkerContext context) 77 | throws Exception 78 | { 79 | super.afterWalk(context); 80 | artifactDiscoveryListener.afterWalk(getRepository()); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/actors/TagActor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.actors; 14 | 15 | import java.io.IOException; 16 | import java.util.Map; 17 | 18 | import javax.inject.Named; 19 | import javax.inject.Singleton; 20 | 21 | import org.sonatype.nexus.examples.selectionactors.Actor; 22 | import org.sonatype.nexus.examples.selectionactors.Selection; 23 | import org.sonatype.nexus.examples.selectionactors.SelectionEntry; 24 | import org.sonatype.nexus.proxy.IllegalOperationException; 25 | import org.sonatype.nexus.proxy.ItemNotFoundException; 26 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 27 | import org.sonatype.nexus.proxy.item.StorageItem; 28 | 29 | /** 30 | * A simple "tag" actor, that "tags" the selected items by setting the supplied attribute value for given attribute 31 | * key. 32 | * 33 | * @since 1.0 34 | */ 35 | @Named("tag") 36 | @Singleton 37 | public class TagActor 38 | implements Actor 39 | { 40 | /** 41 | * The key for tag key term (mandatory). 42 | */ 43 | public static final String TERM_ATTRIBUTE_KEY = "tagAttributeKey"; 44 | 45 | /** 46 | * The key for tag value term, optional (mandatory). 47 | */ 48 | public static final String TERM_ATTRIBUTE_VALUE = "tagAttributeValue"; 49 | 50 | @Override 51 | public int perform(final Selection selection, final Map terms) 52 | throws IOException 53 | { 54 | final String attributeKey = terms.get(TERM_ATTRIBUTE_KEY); 55 | if (attributeKey == null) { 56 | throw new IllegalArgumentException("Term " + TERM_ATTRIBUTE_KEY + " not found or is empty!"); 57 | } 58 | final String attributeValue = terms.get(TERM_ATTRIBUTE_VALUE); 59 | if (attributeValue == null) { 60 | throw new IllegalArgumentException("Term " + TERM_ATTRIBUTE_VALUE + " not found or is empty!"); 61 | } 62 | 63 | int acted = 0; 64 | try { 65 | for (SelectionEntry entry : selection) { 66 | try { 67 | doMark(entry, attributeKey, attributeValue); 68 | acted++; 69 | } 70 | catch (ItemNotFoundException e) { 71 | // neglect and continue 72 | } 73 | } 74 | } 75 | catch (IllegalOperationException e) { 76 | // repo does not allow this, is out of service or so? bail out 77 | } 78 | finally { 79 | selection.close(); 80 | } 81 | 82 | return acted; 83 | } 84 | 85 | // == 86 | 87 | protected void doMark(final SelectionEntry entry, final String key, final String value) 88 | throws IllegalOperationException, ItemNotFoundException, IOException 89 | { 90 | final ResourceStoreRequest request = new ResourceStoreRequest(entry.getPath()); 91 | request.setRequestLocalOnly(true); 92 | final StorageItem item = entry.getRepository().retrieveItem(false, request); 93 | item.getRepositoryItemAttributes().put(key, value); 94 | item.getRepositoryItemUid().getRepository().getAttributesHandler().storeAttributes(item); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /selectionactors/README.md: -------------------------------------------------------------------------------- 1 | 15 | # Nexus Selection + Actors Example Plugin 16 | 17 | Select then act upon. Pluggable "selections" and then pluggable "actors" invoked against selection. 18 | 19 | Exposed over REST API as: 20 | ``` 21 | http://localhost:8081/nexus/service/local/select///? 22 | ``` 23 | 24 | The terms is a series of query parameters, and should be prefixed with `t_`. 25 | 26 | ### Example No1: 27 | 28 | ``` 29 | http://localhost:8081/nexus/service/local/select/central/gav/delete?t_g=log4j&t_a=log4j&t_v=1.2.13 30 | ``` 31 | 32 | Would run against repository with ID "central", perform a GAV selection and would delete the matched selection. GAV selection would happen against groupId="log4j", artifactId="log4j", version="1.2.13". Note: this operation would NOT maintain Maven metadata! 33 | 34 | ### Example No2: 35 | 36 | ``` 37 | http://localhost:8081/nexus/service/local/select/releases/gav/tag?t_g=com.mycorp&t_a=myartifact&t_c=bundle&t_tagAttributeKey=foo&t_tagAttributeValue=bar 38 | ``` 39 | 40 | Would run against repository with ID "releases", perform a GAV selection with groupId="com.mycorp", artifactId="myartifact" and classifier="bundle" and would "tag" them (add key-value to attributes) with "foo=bar" attributes. 41 | 42 | ## Selectors 43 | 44 | Implemented selectors: 45 | 46 | * GAVSelector - receives terms `g`, `a`, `v`, `bv` (base version, matters in case of snapshots as `v` would be timestamped snapshot, while this one would be "X-SNAPSHOT"), `e` (extension) and `c` (classifier). The terms values represents plain Java Regexp values, and this selector works only against Maven repositories. Creates a selection that will contain Maven artifacts matching the given terms. 47 | * TargetSelector - receives terms `targetId`. Works against any kind of repository. Creates a selection that contains all items matching given target. 48 | * TagSelector - receives terms `attributeKey` and (optionally) `attributeValue`. Works against any repository. Creates a selection that contains all items that has attribute with `attributeKey`, and if value given, it's value matched with given value. 49 | 50 | 51 | ## Actors 52 | 53 | Implemented actors: 54 | 55 | * DeleteActor -- simply performs "deleteItem" calls against selected items. No terms. 56 | * TagActor -- receives `tagAttributeKey` and `tagAttributeValue` (both terms are mandatory) and "tags" the items in selection. 57 | 58 | ## Response 59 | 60 | A rather simplistic one for now (for example No1 actually performing deletions): 61 | 62 | ``` 63 | 64 | central 65 | gav 66 | 2 67 | delete 68 | 2 69 | true 70 | 71 | ``` 72 | 73 | ## Todo 74 | 75 | This is just an example. More of extra work would be needed to properly finish this example (like UI for this, other selection imple etc). While this might work in production too, I believe it is not needed to mention it is not supported. Also, due to current naive implementation of "selections" (in-memory), doing this on LARGE repository instances with large selections (or not narrowing the selection to contain few hits) would lead to OOMs. -------------------------------------------------------------------------------- /crawling/src/main/java/org/sonatype/nexus/examples/crawling/internal/FileArtifactDiscoveryListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.crawling.internal; 14 | 15 | import java.io.File; 16 | import java.io.IOException; 17 | import java.io.PrintStream; 18 | import java.util.Date; 19 | 20 | import org.apache.commons.lang.StringUtils; 21 | import org.sonatype.nexus.examples.crawling.ArtifactDiscoveryListener; 22 | import org.sonatype.nexus.proxy.item.StorageItem; 23 | import org.sonatype.nexus.proxy.maven.MavenRepository; 24 | import org.sonatype.nexus.proxy.maven.gav.Gav; 25 | import org.sonatype.nexus.proxy.utils.RepositoryStringUtils; 26 | 27 | import static com.google.common.base.Preconditions.checkNotNull; 28 | 29 | /** 30 | * ??? 31 | * 32 | * @since 1.0 33 | */ 34 | public class FileArtifactDiscoveryListener 35 | implements ArtifactDiscoveryListener 36 | { 37 | private final File reportFile; 38 | 39 | private final PrintStream reportPrinter; 40 | 41 | private long started; 42 | 43 | private long itemsFound; 44 | 45 | private long artifactsFound; 46 | 47 | public FileArtifactDiscoveryListener(final File reportFile) 48 | throws IOException 49 | { 50 | this.reportFile = checkNotNull(reportFile); 51 | 52 | // FIXME: Buffer!!! 53 | this.reportPrinter = new PrintStream(reportFile); 54 | } 55 | 56 | protected File getReportFile() { 57 | return reportFile; 58 | } 59 | 60 | @Override 61 | public void beforeWalk(final MavenRepository mavenRepository) { 62 | started = System.currentTimeMillis(); 63 | println("GAV Scan of repository %s", RepositoryStringUtils.getHumanizedNameString(mavenRepository)); 64 | println("Started at %s", new Date(started)); 65 | println("========"); 66 | println("No. Path (G:A:V:[C]:E)"); 67 | println("========"); 68 | println(""); 69 | } 70 | 71 | @Override 72 | public void onArtifactDiscovery(final MavenRepository mavenRepository, final Gav gav, final StorageItem item) { 73 | // update counters for stats 74 | itemsFound++; 75 | // write report 76 | if (gav != null) { 77 | artifactsFound++; 78 | println("%s. %s (%s)", itemsFound, item.getPath(), gavToString(gav)); 79 | } 80 | else { 81 | println("%s. %s", itemsFound, item.getPath()); 82 | } 83 | } 84 | 85 | @Override 86 | public void afterWalk(final MavenRepository mavenRepository) { 87 | println(""); 88 | println("========"); 89 | println("GAV Scan of repository %s finished in %s seconds.", 90 | RepositoryStringUtils.getHumanizedNameString(mavenRepository), 91 | (System.currentTimeMillis() - started) / 1000l); 92 | println("Discovered total of %s items, out of which %s were Maven artifacts.", itemsFound, artifactsFound); 93 | reportPrinter.flush(); 94 | reportPrinter.close(); 95 | } 96 | 97 | // == 98 | 99 | protected void println(final String format, final Object... args) { 100 | reportPrinter.println(String.format(format, args)); 101 | } 102 | 103 | protected String gavToString(final Gav gav) { 104 | final StringBuilder sb = new StringBuilder(); 105 | sb.append(gav.getGroupId()).append(":").append(gav.getArtifactId()).append(":").append( 106 | gav.getVersion()); 107 | if (!StringUtils.isBlank(gav.getClassifier())) { 108 | sb.append(":").append(gav.getClassifier()); 109 | } 110 | sb.append(gav.getExtension()); 111 | return sb.toString(); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /pro-plugins/stagingrules/src/main/java/org/sonatype/nexus/examples/stagingrules/Maven220BlockingStagingRuleEvaluator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.stagingrules; 14 | 15 | import javax.inject.Inject; 16 | import javax.inject.Named; 17 | import javax.inject.Singleton; 18 | 19 | import com.google.common.base.Preconditions; 20 | import com.sonatype.nexus.staging.StagingManager; 21 | import com.sonatype.nexus.staging.persist.model.CCondition; 22 | import com.sonatype.nexus.staging.persist.model.CStageRepository; 23 | import com.sonatype.nexus.staging.rule.RuleResult; 24 | import com.sonatype.nexus.staging.rule.StagingRule; 25 | import com.sonatype.nexus.staging.rule.StagingRuleEvaluator; 26 | 27 | import org.sonatype.nexus.proxy.NoSuchRepositoryException; 28 | import org.sonatype.sisu.goodies.common.ComponentSupport; 29 | 30 | /** 31 | * Evaluates a staging repository to see if Maven 2.2.0 was used. Maven 2.2.0 generates incorrect signatures. 32 | * 33 | * @since 1.0 34 | */ 35 | @Named(Maven220BlockingStagingRuleType.TYPE_ID) 36 | @Singleton 37 | public class Maven220BlockingStagingRuleEvaluator 38 | extends ComponentSupport 39 | implements StagingRuleEvaluator 40 | { 41 | private final StagingManager stagingManager; 42 | 43 | /** 44 | * The actual user-agent would be something like: Apache-Maven/2.2 (Java 1.6.0_16; Linux 2.6.26-2-amd64) 45 | * maven-artifact/2.2.0 46 | */ 47 | private static String maven220UserAgentPart = "maven-artifact/2.2.0"; 48 | 49 | @Inject 50 | public Maven220BlockingStagingRuleEvaluator(final StagingManager stagingManager) { 51 | this.stagingManager = Preconditions.checkNotNull(stagingManager); 52 | } 53 | 54 | public RuleResult evaluate(StagingRule stagingRule) { 55 | RuleResult result = new RuleResult(stagingRule); 56 | 57 | String stagingRepositoryId = stagingRule.getRepository().getId(); 58 | CStageRepository stagingRepo; 59 | 60 | try { 61 | stagingRepo = this.getStagingRepository(stagingRepositoryId); 62 | } 63 | catch (NoSuchRepositoryException e) { 64 | // this should NEVER happen, we are running this rule against this repo 65 | log.error("Error finding the staging profile while executing rule.", e); 66 | result.addFailure("Invalid Staging Profile: This staging profile could not be found."); 67 | return result; // guard 68 | } 69 | 70 | // again, this should never happen 71 | if (stagingRepo == null) { 72 | result.addFailure("Invalid Staging Profile: This staging repository could not be found."); 73 | return result; // guard 74 | } 75 | 76 | // check if Maven 2.2.0 is used 77 | boolean maven220Used = false; 78 | for (CCondition condition : stagingRepo.getConditions()) { 79 | if (condition.getType().equals(CCondition.TYPE_USER_AGENT)) { 80 | if (condition.getValue().contains(maven220UserAgentPart)) { 81 | maven220Used = true; 82 | } 83 | } 84 | } 85 | 86 | if (maven220Used) { 87 | result.addFailure("Invalid Maven Version: Do not use Maven 2.2.0 signatures are calculated incorrectly."); 88 | } 89 | else { 90 | result.addSuccess("Success: Maven 2.2.0 is not use"); 91 | } 92 | 93 | return result; 94 | } 95 | 96 | private CStageRepository getStagingRepository(String stagingRepositoryId) 97 | throws NoSuchRepositoryException 98 | { 99 | return stagingManager.getStageRepositoryById(stagingRepositoryId); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /crawling/src/main/java/org/sonatype/nexus/examples/crawling/internal/task/CrawlTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.crawling.internal.task; 14 | 15 | import java.io.File; 16 | import java.io.IOException; 17 | 18 | import javax.inject.Inject; 19 | import javax.inject.Named; 20 | 21 | import org.sonatype.nexus.configuration.application.ApplicationConfiguration; 22 | import org.sonatype.nexus.examples.crawling.ArtifactDiscoveryListener; 23 | import org.sonatype.nexus.examples.crawling.GavCollector; 24 | import org.sonatype.nexus.examples.crawling.internal.FileArtifactDiscoveryListener; 25 | import org.sonatype.nexus.proxy.NoSuchRepositoryException; 26 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 27 | import org.sonatype.nexus.proxy.maven.MavenRepository; 28 | import org.sonatype.nexus.scheduling.AbstractNexusRepositoriesPathAwareTask; 29 | 30 | import com.google.common.base.Preconditions; 31 | 32 | /** 33 | * ??? 34 | * 35 | * @since 1.0 36 | */ 37 | @Named(CrawlTaskDescriptor.ID) 38 | public class CrawlTask 39 | extends AbstractNexusRepositoriesPathAwareTask 40 | { 41 | private static final String ACTION = "NEXUS5030"; 42 | 43 | private final ApplicationConfiguration applicationConfiguration; 44 | 45 | private final GavCollector gavCollector; 46 | 47 | @Inject 48 | public CrawlTask(final ApplicationConfiguration applicationConfiguration, final GavCollector gavCollector) { 49 | this.applicationConfiguration = Preconditions.checkNotNull(applicationConfiguration); 50 | this.gavCollector = Preconditions.checkNotNull(gavCollector); 51 | } 52 | 53 | @Override 54 | protected String getRepositoryFieldId() { 55 | return CrawlTaskDescriptor.REPOSITORY_FIELD_ID; 56 | } 57 | 58 | @Override 59 | protected String getRepositoryPathFieldId() { 60 | return CrawlTaskDescriptor.REPOSITORY_PATH_FIELD_ID; 61 | } 62 | 63 | @Override 64 | protected Object doRun() 65 | throws IOException 66 | { 67 | if (getRepositoryId() != null) { 68 | try { 69 | final MavenRepository mavenRepository = 70 | getRepositoryRegistry().getRepositoryWithFacet(getRepositoryId(), MavenRepository.class); 71 | if (mavenRepository != null) { 72 | gavCollector.collectGAVs(createRequest(), mavenRepository, createListener(mavenRepository)); 73 | } 74 | } 75 | catch (NoSuchRepositoryException e) { 76 | getLogger().warn("No MavenRepository with ID={} exists!", getRepositoryId()); 77 | } 78 | } 79 | else { 80 | for (MavenRepository mavenRepository : getRepositoryRegistry().getRepositoriesWithFacet( 81 | MavenRepository.class)) { 82 | gavCollector.collectGAVs(createRequest(), mavenRepository, createListener(mavenRepository)); 83 | } 84 | } 85 | 86 | return null; 87 | } 88 | 89 | @Override 90 | protected String getAction() { 91 | return ACTION; 92 | } 93 | 94 | @Override 95 | protected String getMessage() { 96 | return "Harvesting all the artifacts in Maven repository."; 97 | } 98 | 99 | // == 100 | 101 | protected ResourceStoreRequest createRequest() { 102 | return new ResourceStoreRequest(getResourceStorePath(), true); 103 | } 104 | 105 | protected ArtifactDiscoveryListener createListener(final MavenRepository mavenRepository) 106 | throws IOException 107 | { 108 | return new FileArtifactDiscoveryListener(new File( 109 | applicationConfiguration.getWorkingDirectory("crawling"), mavenRepository.getId() + ".txt")); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /pro-plugins/stagingrules/src/main/java/org/sonatype/nexus/examples/stagingrules/BrokenArtifactStagingRuleEvaluator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.stagingrules; 14 | 15 | import java.util.Map; 16 | 17 | import javax.inject.Inject; 18 | import javax.inject.Named; 19 | import javax.inject.Singleton; 20 | 21 | import com.sonatype.nexus.staging.rule.AbstractStagingRuleEvaluator; 22 | import com.sonatype.nexus.staging.rule.AbstractStagingRuleWalkerProcessor; 23 | import com.sonatype.nexus.staging.rule.RuleResult; 24 | 25 | import org.sonatype.nexus.proxy.item.StorageCollectionItem; 26 | import org.sonatype.nexus.proxy.item.StorageFileItem; 27 | import org.sonatype.nexus.proxy.item.StorageItem; 28 | import org.sonatype.nexus.proxy.maven.gav.Gav; 29 | import org.sonatype.nexus.proxy.maven.gav.GavCalculator; 30 | import org.sonatype.nexus.proxy.repository.Repository; 31 | import org.sonatype.nexus.proxy.walker.WalkerContext; 32 | import org.sonatype.nexus.proxy.walker.WalkerFilter; 33 | 34 | /** 35 | * Checks if an artifacts path contains the word 'broken'. 36 | * 37 | * @since 1.0 38 | */ 39 | @Named(BrokenArtifactStagingRuleType.TYPE_ID) 40 | @Singleton 41 | public class BrokenArtifactStagingRuleEvaluator 42 | extends AbstractStagingRuleEvaluator 43 | { 44 | private final WalkerFilter filter = new AllItemWalkerFilter(); 45 | 46 | @Inject 47 | private GavCalculator gavCalculator; 48 | 49 | @Override 50 | protected WalkerFilter getWalkerFilter() { 51 | return filter; 52 | } 53 | 54 | @Override 55 | protected AbstractStagingRuleWalkerProcessor getWalkerProcessor(Repository repository, 56 | RuleResult ruleEvaluationResult) 57 | { 58 | return new BrokenArtifactStagingRuleWalkerProcessor(repository, ruleEvaluationResult); 59 | } 60 | 61 | private class BrokenArtifactStagingRuleWalkerProcessor 62 | extends AbstractStagingRuleWalkerProcessor 63 | { 64 | public BrokenArtifactStagingRuleWalkerProcessor(Repository repository, RuleResult ruleResult) { 65 | super(repository, ruleResult); 66 | } 67 | 68 | @Override 69 | protected void processFileItem(WalkerContext context, StorageFileItem item) 70 | throws Exception 71 | { 72 | if (item.getPath().contains("broken")) { 73 | getRuleResult().addFailure("Broken Artifact: '" + item.getPath() + ", the artifact is broken."); 74 | } 75 | else { 76 | getRuleResult().addSuccess( 77 | "Succeeded to validate artifact '" + item.getPath() + "' on repository '" + getRepository().getId() + "'."); 78 | } 79 | 80 | } 81 | } 82 | 83 | private class AllItemWalkerFilter 84 | implements WalkerFilter 85 | { 86 | 87 | public boolean shouldProcess(WalkerContext ctx, StorageItem item) { 88 | if (!(item instanceof StorageFileItem)) { 89 | return false; 90 | } 91 | 92 | Gav gav = gavCalculator.pathToGav( item.getPath() ); 93 | 94 | if (gav != null && !gav.isHash() && !gav.isSignature()) { 95 | System.out.println("Group ID: " + gav.getGroupId() + 96 | " Artifact Id : " + gav.getArtifactId() + 97 | " Version: " + gav.getVersion() + 98 | " Classifier: " + gav.getClassifier() + 99 | " Extension: " + gav.getExtension()); 100 | 101 | Map attributesMap = item.getRepositoryItemAttributes().asMap(); 102 | System.out.println("Deployed by: " + attributesMap.get("request.user")); 103 | } 104 | 105 | 106 | // NOTE: we could do the filtering here too 107 | // if ( item.getPath().contains( "broken" ) ) 108 | // { 109 | // return true; 110 | // } 111 | 112 | return true; 113 | } 114 | 115 | public boolean shouldProcessRecursively(WalkerContext ctx, StorageCollectionItem coll) { 116 | return true; 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /selectionactors/src/main/java/org/sonatype/nexus/examples/selectionactors/rest/SelectorActorResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.selectionactors.rest; 14 | 15 | import java.io.IOException; 16 | import java.util.HashMap; 17 | import java.util.Map; 18 | 19 | import javax.inject.Inject; 20 | import javax.inject.Named; 21 | import javax.inject.Singleton; 22 | 23 | import org.sonatype.nexus.examples.selectionactors.Actor; 24 | import org.sonatype.nexus.examples.selectionactors.Selection; 25 | import org.sonatype.nexus.examples.selectionactors.Selector; 26 | import org.sonatype.nexus.examples.selectionactors.model.RunReportDTO; 27 | import org.sonatype.nexus.proxy.NoSuchRepositoryException; 28 | import org.sonatype.nexus.proxy.repository.Repository; 29 | import org.sonatype.nexus.rest.AbstractNexusPlexusResource; 30 | import org.sonatype.plexus.rest.resource.PathProtectionDescriptor; 31 | 32 | import com.google.common.base.Preconditions; 33 | import com.thoughtworks.xstream.XStream; 34 | 35 | import org.restlet.Context; 36 | import org.restlet.data.Form; 37 | import org.restlet.data.Parameter; 38 | import org.restlet.data.Request; 39 | import org.restlet.data.Response; 40 | import org.restlet.data.Status; 41 | import org.restlet.resource.ResourceException; 42 | import org.restlet.resource.Variant; 43 | 44 | /** 45 | * ??? 46 | * 47 | * @since 1.0 48 | */ 49 | @Named 50 | @Singleton 51 | public class SelectorActorResource 52 | extends AbstractNexusPlexusResource 53 | { 54 | private static final String REPOSITORY_ID = "repositoryId"; 55 | 56 | private static final String SELECTOR_ID = "selectorId"; 57 | 58 | private static final String ACTOR_ID = "actorId"; 59 | 60 | private final Map selectors; 61 | 62 | private final Map actors; 63 | 64 | @Inject 65 | public SelectorActorResource(final Map selectors, final Map actors) { 66 | this.selectors = Preconditions.checkNotNull(selectors); 67 | this.actors = Preconditions.checkNotNull(actors); 68 | setReadable(true); 69 | setModifiable(false); 70 | } 71 | 72 | @Override 73 | public String getResourceUri() { 74 | return "/select/{" + REPOSITORY_ID + "}/{" + SELECTOR_ID + "}/{" + ACTOR_ID + "}"; 75 | } 76 | 77 | @Override 78 | public Object getPayloadInstance() { 79 | return null; 80 | } 81 | 82 | @Override 83 | public PathProtectionDescriptor getResourceProtection() { 84 | return new PathProtectionDescriptor("/select/*/*/*", "authcBasic"); 85 | } 86 | 87 | @Override 88 | public void configureXStream(XStream xstream) { 89 | super.configureXStream(xstream); 90 | xstream.processAnnotations(RunReportDTO.class); 91 | } 92 | 93 | public Object get(final Context context, final Request request, final Response response, final Variant variant) 94 | throws ResourceException 95 | { 96 | try { 97 | final Form form = request.getResourceRef().getQueryAsForm(); 98 | final Map terms = new HashMap(); 99 | for (Parameter parameter : form) { 100 | String paramName = parameter.getName(); 101 | if (paramName.startsWith("t_") && paramName.length() > 2) { 102 | terms.put(paramName.substring(2), parameter.getValue()); 103 | } 104 | } 105 | 106 | final String selectorKey = request.getAttributes().get(SELECTOR_ID).toString(); 107 | final Selector selector = selectors.get(selectorKey); 108 | if (selector == null) { 109 | throw new IllegalArgumentException("Selector not found!"); 110 | } 111 | 112 | final String actorKey = request.getAttributes().get(ACTOR_ID).toString(); 113 | final Actor actor = actors.get(actorKey); 114 | if (actor == null) { 115 | throw new IllegalArgumentException("Actor not found!"); 116 | } 117 | 118 | final Repository repository; 119 | try { 120 | repository = 121 | getRepositoryRegistry().getRepository(request.getAttributes().get(REPOSITORY_ID).toString()); 122 | } 123 | catch (NoSuchRepositoryException e) { 124 | throw new IllegalArgumentException("Repository not found!"); 125 | } 126 | 127 | final Selection selection = selector.select(repository, terms); 128 | final int selectionSize = selection.size(); 129 | final int actedSize = actor.perform(selection, terms); 130 | 131 | return new RunReportDTO(repository.getId(), selectorKey, selectionSize, actorKey, actedSize, true); 132 | } 133 | catch (IllegalArgumentException t) { 134 | throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, t); 135 | } 136 | catch (IOException t) { 137 | throw new ResourceException(Status.SERVER_ERROR_INTERNAL, t); 138 | } 139 | } 140 | 141 | } 142 | -------------------------------------------------------------------------------- /virusscan/src/main/java/org/sonatype/nexus/examples/virusscan/VirusScannerRequestProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.virusscan; 14 | 15 | import java.util.List; 16 | 17 | import javax.inject.Inject; 18 | import javax.inject.Named; 19 | 20 | import org.sonatype.nexus.proxy.IllegalOperationException; 21 | import org.sonatype.nexus.proxy.ItemNotFoundException; 22 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 23 | import org.sonatype.nexus.proxy.access.Action; 24 | import org.sonatype.nexus.proxy.item.StorageFileItem; 25 | import org.sonatype.nexus.proxy.item.StorageItem; 26 | import org.sonatype.nexus.proxy.repository.ProxyRepository; 27 | import org.sonatype.nexus.proxy.repository.Repository; 28 | import org.sonatype.nexus.proxy.repository.RequestStrategy; 29 | import org.sonatype.sisu.goodies.common.ComponentSupport; 30 | import org.sonatype.sisu.goodies.eventbus.EventBus; 31 | 32 | import com.google.common.annotations.VisibleForTesting; 33 | import com.google.common.base.Preconditions; 34 | 35 | /** 36 | * Virus scanning {@link org.sonatype.nexus.proxy.repository.RequestStrategy}. 37 | * 38 | * @since 1.0 39 | */ 40 | @Named(VirusScannerRequestProcessor.ID) 41 | public class VirusScannerRequestProcessor 42 | extends ComponentSupport 43 | implements RequestStrategy 44 | { 45 | public static final String ID = "virus-scanner"; 46 | 47 | private final EventBus eventBus; 48 | 49 | private final List scanners; 50 | 51 | @Inject 52 | public VirusScannerRequestProcessor(final EventBus eventBus, 53 | final List scanners) 54 | { 55 | this.eventBus = Preconditions.checkNotNull(eventBus); 56 | this.scanners = Preconditions.checkNotNull(scanners); 57 | 58 | if (scanners.isEmpty()) { 59 | log.warn("No VirusScanner components detected"); 60 | } 61 | else if (log.isDebugEnabled()) { 62 | log.debug("Virus scanners:"); 63 | for (VirusScanner scanner : scanners) { 64 | log.debug(" {}", scanner); 65 | } 66 | } 67 | } 68 | 69 | @VisibleForTesting 70 | boolean hasVirus(final StorageFileItem item) { 71 | log.debug("Scanning item for viruses: {}", item.getPath()); 72 | 73 | boolean infected = false; 74 | for (VirusScanner scanner : scanners) { 75 | if (scanner.hasVirus(item)) { 76 | infected = true; 77 | eventBus.post(new InfectedItemFoundEvent(item.getRepositoryItemUid().getRepository(), item)); 78 | } 79 | } 80 | 81 | if (infected) { 82 | log.warn("Infection detected in item: {}", item.getPath()); 83 | } 84 | 85 | return infected; 86 | } 87 | 88 | // @Override 89 | // public boolean process(final Repository repository, final ResourceStoreRequest request, final Action action) { 90 | // // don't decide until have content 91 | // return true; 92 | // } 93 | // 94 | // @Override 95 | // public boolean shouldProxy(final ProxyRepository repository, final ResourceStoreRequest request) { 96 | // // don't decide until have content 97 | // return true; 98 | // } 99 | // 100 | // @Override 101 | // public boolean shouldRetrieve(final Repository repository, final ResourceStoreRequest request, final StorageItem item) 102 | // throws IllegalOperationException, ItemNotFoundException, AccessDeniedException 103 | // { 104 | // // don't decide until have content 105 | // return true; 106 | // } 107 | // 108 | // @Override 109 | // public boolean shouldCache(final ProxyRepository repository, final AbstractStorageItem item) { 110 | // if (item instanceof StorageFileItem) { 111 | // StorageFileItem file = (StorageFileItem) item; 112 | // return !hasVirus(file); 113 | // } 114 | // else { 115 | // return true; 116 | // } 117 | // } 118 | 119 | @Override 120 | public void onHandle(final Repository repository, final ResourceStoreRequest resourceStoreRequest, 121 | final Action action) 122 | throws ItemNotFoundException, IllegalOperationException 123 | { 124 | } 125 | 126 | @Override 127 | public void onServing(final Repository repository, final ResourceStoreRequest resourceStoreRequest, 128 | final StorageItem storageItem) 129 | throws ItemNotFoundException, IllegalOperationException 130 | { 131 | if (storageItem instanceof StorageFileItem) { 132 | StorageFileItem file = (StorageFileItem) storageItem; 133 | if (hasVirus(file)) { 134 | throw new IllegalStateException("Cannot serve an infected file!"); 135 | } 136 | } 137 | } 138 | 139 | @Override 140 | public void onRemoteAccess(final ProxyRepository proxyRepository, final ResourceStoreRequest resourceStoreRequest, 141 | final StorageItem storageItem) 142 | throws ItemNotFoundException, IllegalOperationException 143 | { 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /url-realm-nexus-plugin/src/main/java/org/sonatype/nexus/examples/url/UrlRealm.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.url; 14 | 15 | import java.io.IOException; 16 | 17 | import javax.enterprise.inject.Typed; 18 | import javax.inject.Inject; 19 | import javax.inject.Named; 20 | import javax.inject.Singleton; 21 | 22 | import org.sonatype.nexus.apachehttpclient.Hc4Provider; 23 | import org.sonatype.nexus.configuration.application.ApplicationConfiguration; 24 | import org.sonatype.nexus.proxy.repository.UsernamePasswordRemoteAuthenticationSettings; 25 | import org.sonatype.nexus.proxy.storage.remote.DefaultRemoteStorageContext; 26 | 27 | import org.apache.http.HttpResponse; 28 | import org.apache.http.client.HttpClient; 29 | import org.apache.http.client.methods.HttpGet; 30 | import org.apache.http.client.utils.HttpClientUtils; 31 | import org.apache.shiro.authc.AuthenticationException; 32 | import org.apache.shiro.authc.AuthenticationInfo; 33 | import org.apache.shiro.authc.AuthenticationToken; 34 | import org.apache.shiro.authc.SimpleAuthenticationInfo; 35 | import org.apache.shiro.authc.UnknownAccountException; 36 | import org.apache.shiro.authc.UsernamePasswordToken; 37 | import org.apache.shiro.authz.AuthorizationInfo; 38 | import org.apache.shiro.authz.SimpleAuthorizationInfo; 39 | import org.apache.shiro.realm.AuthorizingRealm; 40 | import org.apache.shiro.realm.Realm; 41 | import org.apache.shiro.subject.PrincipalCollection; 42 | import org.eclipse.sisu.Description; 43 | import org.slf4j.Logger; 44 | import org.slf4j.LoggerFactory; 45 | 46 | import static com.google.common.base.Preconditions.checkNotNull; 47 | 48 | /** 49 | * A Realm that performs HTTP GET authenticated with user entered credentials against a remote URL to perform authc. 50 | */ 51 | @Singleton 52 | @Typed(Realm.class) 53 | @Named(UrlRealm.NAME) 54 | @Description("URL Realm") 55 | public class UrlRealm 56 | extends AuthorizingRealm 57 | { 58 | public static final String NAME = "URLRealm"; 59 | 60 | private static final Logger logger = LoggerFactory.getLogger(UrlRealm.class); 61 | 62 | private final ApplicationConfiguration applicationConfiguration; 63 | 64 | private final Hc4Provider hc4Provider; 65 | 66 | private final String baseUrl; 67 | 68 | private final String defaultRole; 69 | 70 | @Inject 71 | public UrlRealm(final ApplicationConfiguration applicationConfiguration, 72 | final Hc4Provider hc4Provider, 73 | @Named("${nexus.urlrealm.baseUrl}") final String baseUrl, 74 | @Named("${nexus.urlrealm.defaultRole}") final String defaultRole) 75 | { 76 | this.applicationConfiguration = checkNotNull(applicationConfiguration); 77 | this.hc4Provider = checkNotNull(hc4Provider); 78 | this.baseUrl = checkNotNull(baseUrl); 79 | this.defaultRole = checkNotNull(defaultRole); 80 | setName(NAME); 81 | setAuthenticationCachingEnabled(true); 82 | setAuthorizationCachingEnabled(true); 83 | } 84 | 85 | @Override 86 | public boolean supports(final AuthenticationToken token) { 87 | return (token instanceof UsernamePasswordToken); 88 | } 89 | 90 | @Override 91 | protected AuthenticationInfo doGetAuthenticationInfo(final AuthenticationToken token) 92 | throws AuthenticationException 93 | { 94 | final UsernamePasswordToken upToken = (UsernamePasswordToken) token; 95 | 96 | // if the user can authenticate we are good to go 97 | if (authenticateViaUrl(upToken)) { 98 | return buildAuthenticationInfo(upToken); 99 | } 100 | else { 101 | throw new UnknownAccountException("User \"" + upToken.getUsername() 102 | + "\" cannot be authenticated against URL Realm."); 103 | } 104 | } 105 | 106 | private AuthenticationInfo buildAuthenticationInfo(final UsernamePasswordToken token) { 107 | return new SimpleAuthenticationInfo(token.getPrincipal(), token.getCredentials(), getName()); 108 | } 109 | 110 | private boolean authenticateViaUrl(final UsernamePasswordToken usernamePasswordToken) { 111 | final DefaultRemoteStorageContext ctx = new DefaultRemoteStorageContext( 112 | applicationConfiguration.getGlobalRemoteStorageContext()); 113 | ctx.setRemoteAuthenticationSettings(new UsernamePasswordRemoteAuthenticationSettings( 114 | usernamePasswordToken.getUsername(), new String(usernamePasswordToken.getPassword()))); 115 | final HttpClient client = hc4Provider.createHttpClient(ctx); 116 | try { 117 | final HttpResponse response = client.execute(new HttpGet(baseUrl)); 118 | 119 | try { 120 | logger.debug("URL Realm user \"{}\" validated against URL={} as {}", usernamePasswordToken.getUsername(), baseUrl, 121 | response.getStatusLine()); 122 | final boolean success = 123 | response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() <= 299; 124 | return success; 125 | } 126 | finally { 127 | HttpClientUtils.closeQuietly(response); 128 | } 129 | } 130 | catch (IOException e) { 131 | logger.info("URL Realm was unable to perform authentication", e); 132 | return false; 133 | } 134 | } 135 | 136 | @Override 137 | protected AuthorizationInfo doGetAuthorizationInfo(final PrincipalCollection principals) { 138 | // only if authenticated with this realm too 139 | if (!principals.getRealmNames().contains(getName())) { 140 | return null; 141 | } 142 | // add the default role 143 | final SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); 144 | authorizationInfo.addRole(defaultRole); 145 | return authorizationInfo; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 19 | 4.0.0 20 | 21 | 22 | org.sonatype.nexus.plugins 23 | nexus-plugins 24 | 2.11.4-SNAPSHOT 25 | 26 | 27 | org.sonatype.nexus.examples 28 | nexus-examples 29 | ${project.groupId}:${project.artifactId} 30 | pom 31 | 32 | 1.0-SNAPSHOT 33 | 34 | 35 | 36 | rso-public-grid 37 | https://repository.sonatype.org/content/groups/sonatype-public-grid/ 38 | 39 | 40 | 41 | 42 | attributes 43 | crawling 44 | selectionactors 45 | virusscan 46 | url-realm-nexus-plugin 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.sonatype.nexus.examples 58 | attributes-nexus-plugin 59 | nexus-plugin 60 | 1.0-SNAPSHOT 61 | 62 | 63 | 64 | org.sonatype.nexus.examples 65 | attributes-nexus-plugin 66 | jar 67 | 1.0-SNAPSHOT 68 | 69 | 70 | 71 | org.sonatype.nexus.examples 72 | attributes-nexus-plugin 73 | zip 74 | bundle 75 | 1.0-SNAPSHOT 76 | 77 | 78 | 79 | 80 | 81 | org.sonatype.nexus.examples 82 | crawling-nexus-plugin 83 | nexus-plugin 84 | 1.0-SNAPSHOT 85 | 86 | 87 | 88 | org.sonatype.nexus.examples 89 | crawling-nexus-plugin 90 | jar 91 | 1.0-SNAPSHOT 92 | 93 | 94 | 95 | 96 | 97 | org.sonatype.nexus.examples 98 | selectionactors-nexus-plugin 99 | nexus-plugin 100 | 1.0-SNAPSHOT 101 | 102 | 103 | 104 | org.sonatype.nexus.examples 105 | selectionactors-nexus-plugin 106 | jar 107 | 1.0-SNAPSHOT 108 | 109 | 110 | 111 | 112 | 113 | org.sonatype.nexus.examples 114 | virusscan-nexus-plugin 115 | nexus-plugin 116 | 1.0-SNAPSHOT 117 | 118 | 119 | 120 | org.sonatype.nexus.examples 121 | virusscan-nexus-plugin 122 | jar 123 | 1.0-SNAPSHOT 124 | 125 | 126 | 127 | 128 | 129 | org.sonatype.nexus.examples 130 | stagingrules-nexus-plugin 131 | nexus-plugin 132 | 1.0-SNAPSHOT 133 | 134 | 135 | 136 | org.sonatype.nexus.examples 137 | stagingrules-nexus-plugin 138 | jar 139 | 1.0-SNAPSHOT 140 | 141 | 142 | 143 | 144 | 145 | org.sonatype.nexus.examples 146 | url-realm-nexus-plugin 147 | nexus-plugin 148 | 1.0-SNAPSHOT 149 | 150 | 151 | 152 | org.sonatype.nexus.examples 153 | url-realm-nexus-plugin 154 | jar 155 | 1.0-SNAPSHOT 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 166 | 167 | org.sonatype.nexus 168 | nexus-plugin-bundle-maven-plugin 169 | true 170 | 171 | 172 | 173 | 174 | 175 | 178 | 179 | pro-plugins 180 | 181 | pro-plugins 182 | 183 | 184 | 185 | 186 | 187 | -------------------------------------------------------------------------------- /attributes/src/main/java/org/sonatype/nexus/examples/attributes/rest/ItemAttributesResource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.attributes.rest; 14 | 15 | import java.util.Map; 16 | 17 | import javax.inject.Named; 18 | import javax.inject.Singleton; 19 | 20 | import org.sonatype.nexus.examples.attributes.model.AttributeDTO; 21 | import org.sonatype.nexus.examples.attributes.model.AttributesDTO; 22 | import org.sonatype.nexus.proxy.NoSuchRepositoryException; 23 | import org.sonatype.nexus.proxy.ResourceStoreRequest; 24 | import org.sonatype.nexus.proxy.attributes.Attributes; 25 | import org.sonatype.nexus.proxy.item.StorageItem; 26 | import org.sonatype.nexus.proxy.repository.Repository; 27 | import org.sonatype.nexus.rest.AbstractResourceStoreContentPlexusResource; 28 | import org.sonatype.plexus.rest.resource.PathProtectionDescriptor; 29 | 30 | import com.google.common.annotations.VisibleForTesting; 31 | import com.thoughtworks.xstream.XStream; 32 | import org.restlet.Context; 33 | import org.restlet.data.Request; 34 | import org.restlet.data.Response; 35 | import org.restlet.data.Status; 36 | import org.restlet.resource.ResourceException; 37 | import org.restlet.resource.Variant; 38 | 39 | import static com.google.common.base.Preconditions.checkArgument; 40 | import static org.sonatype.nexus.rest.repositories.AbstractRepositoryPlexusResource.REPOSITORY_ID_KEY; 41 | 42 | /** 43 | * Item attributes REST resource. 44 | * 45 | * @since 1.0 46 | */ 47 | @Named 48 | @Singleton 49 | public class ItemAttributesResource 50 | extends AbstractResourceStoreContentPlexusResource 51 | { 52 | public static final String SYSTEM_ATTR_PREFIX = "storageItem-"; 53 | 54 | public ItemAttributesResource() { 55 | // enable GET 56 | setReadable(true); 57 | 58 | // enable PUT and DELETE (even though DELETE is not supported) 59 | setModifiable(true); 60 | } 61 | 62 | @Override 63 | public String getResourceUri() { 64 | return "/repositories/{" + REPOSITORY_ID_KEY + "}/attributes"; 65 | } 66 | 67 | @Override 68 | public AttributesDTO getPayloadInstance() { 69 | return new AttributesDTO(); 70 | } 71 | 72 | @Override 73 | public boolean acceptsUpload() { 74 | // we handle PUT method only 75 | return false; 76 | } 77 | 78 | @Override 79 | public PathProtectionDescriptor getResourceProtection() { 80 | return new PathProtectionDescriptor("/repositories/*/attributes/**", "authcBasic"); 81 | } 82 | 83 | @Override 84 | public void configureXStream(final XStream xstream) { 85 | super.configureXStream(xstream); 86 | xstream.processAnnotations(AttributeDTO.class); 87 | xstream.processAnnotations(AttributesDTO.class); 88 | } 89 | 90 | @Override 91 | protected Repository getResourceStore(final Request request) 92 | throws NoSuchRepositoryException 93 | { 94 | return getUnprotectedRepositoryRegistry().getRepository( 95 | request.getAttributes().get(REPOSITORY_ID_KEY).toString()); 96 | } 97 | 98 | @VisibleForTesting 99 | static void applyTo(final AttributesDTO attributes, final Attributes proxyAttributes) { 100 | for (AttributeDTO attribute : attributes) { 101 | String key = attribute.getKey(); 102 | checkArgument(!key.startsWith(SYSTEM_ATTR_PREFIX), "Can not override system attribute: %s", key); 103 | proxyAttributes.put(key, attribute.getValue()); 104 | } 105 | } 106 | 107 | @VisibleForTesting 108 | static AttributesDTO buildFrom(final Attributes proxyAttributes) { 109 | Map attributesMap = proxyAttributes.asMap(); 110 | AttributesDTO result = new AttributesDTO(); 111 | for (Map.Entry entry : attributesMap.entrySet()) { 112 | AttributeDTO attribute = new AttributeDTO(entry.getKey(), entry.getValue()); 113 | result.getAttributes().add(attribute); 114 | } 115 | return result; 116 | } 117 | 118 | // FIXME: Replace with siesta + JAX-RS 119 | 120 | @Override 121 | public Object get(Context context, Request request, Response response, Variant variant) 122 | throws ResourceException 123 | { 124 | try { 125 | // create request 126 | final ResourceStoreRequest req = getResourceStoreRequest(request); 127 | 128 | // retrieve the item 129 | final StorageItem item = getResourceStore(request).retrieveItem(req); 130 | 131 | // build and respond with attributes 132 | return buildFrom(item.getRepositoryItemAttributes()); 133 | } 134 | catch (Exception e) { 135 | handleException(request, response, e); 136 | return null; 137 | } 138 | } 139 | 140 | @Override 141 | public Object put(Context context, Request request, Response response, Object payload) 142 | throws ResourceException 143 | { 144 | // PUT has attributes as payload 145 | final AttributesDTO attributes = (AttributesDTO) payload; 146 | 147 | try { 148 | // create request 149 | final ResourceStoreRequest req = getResourceStoreRequest(request); 150 | 151 | // retrieve the item 152 | final StorageItem item = getResourceStore(request).retrieveItem(req); 153 | 154 | // apply the payload attributes to item attributes 155 | applyTo(attributes, item.getRepositoryItemAttributes()); 156 | 157 | // use handler to persist/save the attributes 158 | getResourceStore(request).getAttributesHandler().storeAttributes(item); 159 | 160 | // return the new state (rebuild from modified attributes) 161 | return buildFrom(item.getRepositoryItemAttributes()); 162 | } 163 | catch (Exception e) { 164 | handleException(request, response, e); 165 | return null; 166 | } 167 | } 168 | 169 | @Override 170 | public void delete(Context context, Request request, Response response) 171 | throws ResourceException 172 | { 173 | // override what parent does, for attributes, it is not allowed for now 174 | throw new ResourceException(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /url-realm-nexus-plugin/src/test/java/org/sonatype/nexus/examples/url/UrlRealmTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2014 Sonatype, Inc. All rights reserved. 3 | * 4 | * This program is licensed to you under the Apache License Version 2.0, 5 | * and you may not use this file except in compliance with the Apache License Version 2.0. 6 | * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. 7 | * 8 | * Unless required by applicable law or agreed to in writing, 9 | * software distributed under the Apache License Version 2.0 is distributed on an 10 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. 12 | */ 13 | package org.sonatype.nexus.examples.url; 14 | 15 | import java.util.Map; 16 | 17 | import javax.servlet.http.HttpServletRequest; 18 | import javax.servlet.http.HttpServletResponse; 19 | 20 | import org.sonatype.nexus.apachehttpclient.Hc4Provider; 21 | import org.sonatype.nexus.configuration.application.ApplicationConfiguration; 22 | import org.sonatype.nexus.proxy.storage.remote.DefaultRemoteStorageContext; 23 | import org.sonatype.nexus.proxy.storage.remote.RemoteStorageContext; 24 | import org.sonatype.sisu.litmus.testsupport.TestSupport; 25 | import org.sonatype.tests.http.server.api.Behaviour; 26 | import org.sonatype.tests.http.server.fluent.Behaviours; 27 | import org.sonatype.tests.http.server.fluent.Server; 28 | 29 | import org.apache.http.auth.AuthScope; 30 | import org.apache.http.auth.UsernamePasswordCredentials; 31 | import org.apache.http.impl.client.BasicCredentialsProvider; 32 | import org.apache.http.impl.client.CloseableHttpClient; 33 | import org.apache.http.impl.client.HttpClients; 34 | import org.apache.shiro.authc.AuthenticationInfo; 35 | import org.apache.shiro.authc.UnknownAccountException; 36 | import org.apache.shiro.authc.UsernamePasswordToken; 37 | import org.apache.shiro.authz.UnauthorizedException; 38 | import org.apache.shiro.subject.SimplePrincipalCollection; 39 | import org.eclipse.jetty.util.B64Code; 40 | import org.junit.After; 41 | import org.junit.Before; 42 | import org.junit.Test; 43 | import org.mockito.Mock; 44 | import org.mockito.Mockito; 45 | 46 | import static org.hamcrest.MatcherAssert.*; 47 | import static org.hamcrest.Matchers.*; 48 | import static org.mockito.Mockito.when; 49 | 50 | public class UrlRealmTest 51 | extends TestSupport 52 | { 53 | protected static final String username = "popeye"; 54 | 55 | protected static final String password = "spinach"; 56 | 57 | protected static final String DEFAULT_ROLE = "default-url-role"; 58 | 59 | @Mock 60 | protected ApplicationConfiguration applicationConfiguration; 61 | 62 | @Mock 63 | protected Hc4Provider hc4Provider; 64 | 65 | protected Server server; 66 | 67 | protected CloseableHttpClient httpClient; 68 | 69 | protected UrlRealm urlRealm; 70 | 71 | /** 72 | * HTTP Basic auth, copied and fixed from org.sonatype.tests.http.server.jetty.behaviour.BasicAuth 73 | * Fixed to challenge if 401 is the response, hence, properly implements HTTP Basic, unlike the one from Testsuite. 74 | */ 75 | public static class BasicAuth 76 | implements Behaviour 77 | { 78 | private final String password; 79 | 80 | private final String user; 81 | 82 | public BasicAuth(String user, String password) { 83 | this.user = user; 84 | this.password = password; 85 | } 86 | 87 | public boolean execute(HttpServletRequest request, HttpServletResponse response, Map ctx) 88 | throws Exception 89 | { 90 | String userPass = new String(B64Code.encode((user + ":" + password).getBytes("UTF-8"))); 91 | if (("Basic " + userPass).equals(request.getHeader("Authorization"))) { 92 | return true; 93 | } 94 | response.setHeader("WWW-Authenticate", "Basic realm=\"Secure\""); 95 | response.sendError(401, "not authorized"); 96 | return false; 97 | } 98 | } 99 | 100 | @Before 101 | public void prepare() throws Exception { 102 | server = Server.server().port(0).serve("/*").withBehaviours(new BasicAuth(username, password), 103 | Behaviours.content("not important")); 104 | server.start(); 105 | 106 | // prepare URLRealm 107 | when(applicationConfiguration.getGlobalRemoteStorageContext()).thenReturn(new DefaultRemoteStorageContext(null)); 108 | 109 | urlRealm = new UrlRealm(applicationConfiguration, hc4Provider, server.getUrl().toString() + "/foo", DEFAULT_ROLE); 110 | } 111 | 112 | @After 113 | public void cleanUp() throws Exception { 114 | try { 115 | if (httpClient != null) { 116 | httpClient.close(); 117 | } 118 | } 119 | finally { 120 | server.stop(); 121 | } 122 | } 123 | 124 | // == 125 | 126 | @Test 127 | public void testAuthc() 128 | throws Exception 129 | { 130 | // build client 131 | BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); 132 | credsProvider.setCredentials( 133 | AuthScope.ANY, 134 | new UsernamePasswordCredentials(username, password)); 135 | httpClient = HttpClients.custom() 136 | .setDefaultCredentialsProvider(credsProvider) 137 | .build(); 138 | when(hc4Provider.createHttpClient(Mockito.any(RemoteStorageContext.class))).thenReturn(httpClient); 139 | 140 | final AuthenticationInfo info = urlRealm.getAuthenticationInfo(new UsernamePasswordToken(username, password)); 141 | assertThat(info, notNullValue()); 142 | } 143 | 144 | @Test(expected = UnknownAccountException.class) 145 | public void testAuthcWrongCreds() 146 | throws Exception 147 | { 148 | // build client 149 | BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); 150 | credsProvider.setCredentials( 151 | AuthScope.ANY, 152 | new UsernamePasswordCredentials(username, "cake")); 153 | httpClient = HttpClients.custom() 154 | .setDefaultCredentialsProvider(credsProvider) 155 | .build(); 156 | when(hc4Provider.createHttpClient(Mockito.any(RemoteStorageContext.class))).thenReturn(httpClient); 157 | 158 | urlRealm.getAuthenticationInfo(new UsernamePasswordToken(username, "cake")); 159 | } 160 | 161 | @Test(expected = UnknownAccountException.class) 162 | public void testAuthcJunkCreds() 163 | throws Exception 164 | { 165 | // build client 166 | BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); 167 | credsProvider.setCredentials( 168 | AuthScope.ANY, 169 | new UsernamePasswordCredentials("fakeuser", "hack-me-in")); 170 | httpClient = HttpClients.custom() 171 | .setDefaultCredentialsProvider(credsProvider) 172 | .build(); 173 | when(hc4Provider.createHttpClient(Mockito.any(RemoteStorageContext.class))).thenReturn(httpClient); 174 | 175 | urlRealm.getAuthenticationInfo(new UsernamePasswordToken("fakeuser", "hack-me-in")); 176 | } 177 | 178 | @Test 179 | public void testAuthz() 180 | throws Exception 181 | { 182 | urlRealm.checkRole(new SimplePrincipalCollection(username, urlRealm.getName()), DEFAULT_ROLE); 183 | } 184 | 185 | @Test(expected = UnauthorizedException.class) 186 | public void testAuthzWrongRole() 187 | throws Exception 188 | { 189 | urlRealm.checkRole(new SimplePrincipalCollection(username, urlRealm.getName()), "Not-Existing-Role"); 190 | } 191 | } 192 | --------------------------------------------------------------------------------