Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;

import static org.commonjava.indy.bind.jaxrs.util.JaxRsUriFormatter.getBaseUrlByStoreKey;
import static org.commonjava.indy.bind.jaxrs.util.ResponseUtils.throwError;
import static org.commonjava.indy.koji.model.IndyKojiConstants.ALL_MASKS;
import static org.commonjava.indy.koji.model.IndyKojiConstants.MASK;
Expand Down Expand Up @@ -86,15 +87,8 @@ public KojiRepairResult repairVolumes( final KojiRepairRequest request, final @C
{
try
{
PackageTypeDescriptor packageTypeDescriptor =
PackageTypes.getPackageTypeDescriptor( request.getSource().getPackageType() );

String user = securityManager.getUser( securityContext, servletRequest );
final String baseUrl = uriInfo.getBaseUriBuilder()
.path( packageTypeDescriptor.getContentRestBasePath() )
.build( request.getSource().getType().singularEndpointName(),
request.getSource().getName() )
.toString();
final String baseUrl = getBaseUrlByStoreKey( uriInfo, request.getSource() );
return repairManager.repairVol( request, user, baseUrl );
}
catch ( KojiRepairException | KojiClientException e )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ParsablePom implements ValidationRule {
def errors = Collections.synchronizedList(new ArrayList());
def tools = request.getTools()
def logger = LoggerFactory.getLogger(ValidationRule.class)
logger.info("Parsing POMs in:\n {}.", request.getSourcePaths().join("\n "))
logger.info("Parsing POMs in:\n {}", request.getSourcePaths().join("\n "))

tools.paralleledEach(request.getSourcePaths(), { it ->
if (it.endsWith(".pom")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ private ArtifactStore getRequestStore( PromoteRequest promoteRequest, String bas
{
final ArtifactStore store;
final Logger logger = LoggerFactory.getLogger( getClass() );
logger.info( "Promotion baseUrl: {} ", baseUrl );

if ( needTempRepo( promoteRequest ) )
{
logger.info( "Promotion temporary repo is needed for {}, points to {} ", promoteRequest.getSource(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected ValidationRuleSet getRuleSet()
{
ValidationRuleSet ruleSet = new ValidationRuleSet();
ruleSet.setName( "test" );
ruleSet.setStoreKeyPattern( new StoreKey( group, TARGET_NAME ).toString() );
ruleSet.setStoreKeyPattern( "maven:[^:]+:" + TARGET_NAME );
ruleSet.setRuleNames( Collections.singletonList( getRuleScriptFile() ) );

return ruleSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.promote.ftest.rule;

import org.commonjava.indy.ftest.core.category.EventDependent;
import org.commonjava.indy.model.core.HostedRepository;
import org.commonjava.indy.promote.model.PathsPromoteRequest;
import org.commonjava.indy.promote.model.PathsPromoteResult;
import org.commonjava.indy.promote.model.ValidationResult;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public class ParsablePomRuleByPathTest
extends AbstractValidationRuleTest<HostedRepository>
{

private static final String RULE = "parsable-pom.groovy";

@Test
@Category( EventDependent.class )
public void run() throws Exception
{
String invalid = "org/foo/invalid/1/invalid-1.pom";
String valid = "org/foo/valid/1/valid-1.pom";

deploy( invalid, "This is not parsable" );
deploy( valid, "<?xml version=\"1.0\"?>\n<project><modelVersion>4.0.0</modelVersion><groupId>org.foo</groupId>"
+ "<artifactId>valid</artifactId><version>1</version></project>" );

waitForEventPropagation();

PathsPromoteRequest request = new PathsPromoteRequest( source.getKey(), target.getKey() );
request.setPaths( new HashSet( Arrays.asList( invalid, valid )) );

PathsPromoteResult result = module.promoteByPath( request );
assertThat( result, notNullValue() );

ValidationResult validations = result.getValidations();
assertThat( validations, notNullValue() );

Map<String, String> validatorErrors = validations.getValidatorErrors();
assertThat( validatorErrors, notNullValue() );

System.out.println( validatorErrors );

String errors = validatorErrors.get( RULE );
assertThat( errors, notNullValue() );

System.out.println( validatorErrors );
assertThat( errors.contains( valid ), equalTo( false ) );
assertThat( errors.contains( invalid ), equalTo( true ) );
}

public ParsablePomRuleByPathTest()
{
super( HostedRepository.class );
}

@Override
protected String getRuleScriptFile()
{
return RULE;
}

@Override
protected String getRuleScriptContent() throws IOException
{
String path = "promote/rules/" + RULE;
return readTestResource( path );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.commonjava.indy.promote.bind.jaxrs;

import static org.commonjava.indy.bind.jaxrs.util.JaxRsUriFormatter.getBaseUrlByStoreKey;
import static org.commonjava.indy.util.ApplicationContent.application_json;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -26,8 +27,6 @@
import org.commonjava.indy.IndyWorkflowException;
import org.commonjava.indy.bind.jaxrs.IndyResources;
import org.commonjava.indy.bind.jaxrs.SecurityManager;
import org.commonjava.indy.model.core.PackageTypeDescriptor;
import org.commonjava.indy.model.core.PackageTypes;
import org.commonjava.indy.promote.data.PromotionException;
import org.commonjava.indy.promote.data.PromotionManager;
import org.commonjava.indy.promote.model.GroupPromoteRequest;
Expand Down Expand Up @@ -87,18 +86,12 @@ public GroupPromoteResult promoteToGroup( final GroupPromoteRequest request,
final @Context SecurityContext securityContext,
final @Context UriInfo uriInfo)
{
Response response = null;
Response response;
try
{
PackageTypeDescriptor packageTypeDescriptor =
PackageTypes.getPackageTypeDescriptor( request.getSource().getPackageType() );

String user = securityManager.getUser( securityContext, servletRequest );
final String baseUrl = uriInfo.getBaseUriBuilder()
.path( packageTypeDescriptor.getContentRestBasePath() )
.build( request.getSource().getType().singularEndpointName(),
request.getSource().getName() )
.toString();
final String baseUrl = getBaseUrlByStoreKey( uriInfo, request.getSource() );

return manager.promoteToGroup( request, user, baseUrl );
}
catch ( PromotionException | IndyWorkflowException e )
Expand Down Expand Up @@ -148,8 +141,8 @@ public GroupPromoteResult rollbackGroupPromote( final GroupPromoteResult result,
@Consumes( ApplicationContent.application_json )
public Response promotePaths( final @Context HttpServletRequest request, final @Context UriInfo uriInfo )
{
PathsPromoteRequest req = null;
Response response = null;
PathsPromoteRequest req;
Response response;
try
{
final String json = IOUtils.toString( request.getInputStream() );
Expand All @@ -159,27 +152,16 @@ public Response promotePaths( final @Context HttpServletRequest request, final @
catch ( final IOException e )
{
response = formatResponse( e, "Failed to read DTO from request body." );
}

if ( response != null )
{
return response;
}

try
{
PackageTypeDescriptor packageTypeDescriptor =
PackageTypes.getPackageTypeDescriptor( req.getSource().getPackageType() );

final String baseUrl = uriInfo.getBaseUriBuilder()
.path( packageTypeDescriptor.getContentRestBasePath() )
.build( req.getSource().getType().singularEndpointName(),
req.getSource().getName() )
.toString();
final String baseUrl = getBaseUrlByStoreKey( uriInfo, req.getSource() );
final PathsPromoteResult result = manager.promotePaths( req, baseUrl );

// TODO: Amend response status code based on presence of error? This would have consequences for client API...
response = formatOkResponseWithJsonEntity( result, mapper );
logger.info( "Send promotion result:\n{}", response.getEntity() );
}
catch ( PromotionException | IndyWorkflowException e )
{
Expand All @@ -201,39 +183,26 @@ public Response promotePaths( final @Context HttpServletRequest request, final @
@Consumes( ApplicationContent.application_json )
public Response resumePaths( @Context final HttpServletRequest request, final @Context UriInfo uriInfo )
{
PathsPromoteResult result = null;
Response response = null;
PathsPromoteResult result;
Response response;
try
{
result = mapper.readValue( request.getInputStream(), PathsPromoteResult.class );
}
catch ( final IOException e )
{
response = formatResponse( e, "Failed to read DTO from request body." );
}

if ( response != null )
{
return response;
}

try
{
PathsPromoteRequest req = result.getRequest();

PackageTypeDescriptor packageTypeDescriptor =
PackageTypes.getPackageTypeDescriptor( req.getSource().getPackageType() );

final String baseUrl = uriInfo.getBaseUriBuilder()
.path( packageTypeDescriptor.getContentRestBasePath() )
.build( req.getSource().getType().singularEndpointName(),
req.getSource().getName() )
.toString();
final String baseUrl = getBaseUrlByStoreKey( uriInfo, req.getSource() );

result = manager.resumePathsPromote( result, baseUrl );

// TODO: Amend response status code based on presence of error? This would have consequences for client API...
response = formatOkResponseWithJsonEntity( result, mapper );
logger.info( "Send promotion result:\n{}", response.getEntity() );
}
catch ( PromotionException | IndyWorkflowException e )
{
Expand All @@ -255,27 +224,21 @@ public Response resumePaths( @Context final HttpServletRequest request, final @C
@Consumes( ApplicationContent.application_json )
public Response rollbackPaths( @Context final HttpServletRequest request, @Context final UriInfo uriInfo )
{
PathsPromoteResult result = null;
Response response = null;
PathsPromoteResult result;
Response response;
try
{
result = mapper.readValue( request.getInputStream(), PathsPromoteResult.class );
}
catch ( final IOException e )
{
response = formatResponse( e, "Failed to read DTO from request body." );
}

if ( response != null )
{
return response;
}

try
{
result = manager.rollbackPathsPromote( result );

// TODO: Amend response status code based on presence of error? This would have consequences for client API...
response = formatOkResponseWithJsonEntity( result, mapper );
}
catch ( PromotionException | IndyWorkflowException e )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@

import java.net.MalformedURLException;

import org.commonjava.indy.model.core.PackageTypeDescriptor;
import org.commonjava.indy.model.core.PackageTypes;
import org.commonjava.indy.model.core.StoreKey;
import org.commonjava.indy.util.UriFormatter;
import org.commonjava.atlas.maven.ident.util.JoinString;
import org.commonjava.maven.galley.util.PathUtils;
import org.commonjava.maven.galley.util.UrlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.UriInfo;

public class JaxRsUriFormatter
implements UriFormatter
implements UriFormatter
{
private final Logger logger = LoggerFactory.getLogger( getClass() );

Expand Down Expand Up @@ -55,31 +60,16 @@ public String formatAbsolutePathTo( final String base, final String... parts )
logger.debug( "Resulting URL: '{}'", url );

return url;

// URL baseUrl = null;
// String path = base;
// try
// {
// baseUrl = new URL( base );
// path = baseUrl.getPath();
// }
// catch ( MalformedURLException e )
// {
// // not a URL.
// }
//
// path = PathUtils.normalize( base, PathUtils.normalize( parts ) );
// if ( !path.startsWith( "/" ) )
// {
// path = "/" + path;
// }
//
// if ( baseUrl != null )
// {
// // reconstruct...
// }
//
// return path;
}

public static String getBaseUrlByStoreKey( UriInfo uriInfo, StoreKey storeKey )
{
PackageTypeDescriptor typeDescriptor = PackageTypes.getPackageTypeDescriptor( storeKey.getPackageType() );
return uriInfo.getBaseUriBuilder()
.path( typeDescriptor.getContentRestBasePath() )
.path( storeKey.getType().singularEndpointName() )
.path( storeKey.getName() )
.build()
.toString();
}
}