Migrating return values of rest services away from simple types

This commit is contained in:
Martin Stockhammer 2020-06-30 13:24:49 +02:00
parent b9316745df
commit bef02ef5b8
13 changed files with 251 additions and 37 deletions

View File

@ -0,0 +1,48 @@
package org.apache.archiva.rest.api.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
@XmlRootElement(name="memoryStatus")
public class MemoryStatus
{
String statusText;
public MemoryStatus() {
}
public MemoryStatus(String statusText) {
this.statusText = statusText;
}
public String getStatusText( )
{
return statusText;
}
public void setStatusText( String statusText )
{
this.statusText = statusText;
}
}

View File

@ -0,0 +1,48 @@
package org.apache.archiva.rest.api.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
@XmlRootElement(name="pingResult")
public class PingResult
{
String output;
public PingResult() {
}
public PingResult(String output) {
this.output = output;
}
public String getOutput( )
{
return output;
}
public void setOutput( String output )
{
this.output = output;
}
}

View File

@ -0,0 +1,48 @@
package org.apache.archiva.rest.api.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
@XmlRootElement(name="pomSnippet")
public class PomSnippet
{
String text;
public PomSnippet() {
}
public PomSnippet(String text) {
this.text = text;
}
public String getText( )
{
return text;
}
public void setText( String text )
{
this.text = text;
}
}

View File

@ -0,0 +1,48 @@
package org.apache.archiva.rest.api.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
@XmlRootElement(name="timestamp")
public class Timestamp
{
String value;
public Timestamp() {
}
public Timestamp( String value) {
this.value = value;
}
public String getValue( )
{
return value;
}
public void setValue( String value )
{
this.value = value;
}
}

View File

@ -28,6 +28,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import java.util.Map;
/** /**
* contains some "free" services (i18n) * contains some "free" services (i18n)
@ -47,9 +48,9 @@ public interface CommonServices
*/ */
@Path( "getI18nResources" ) @Path( "getI18nResources" )
@GET @GET
@Produces( { MediaType.TEXT_PLAIN } ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( noRestriction = true ) @RedbackAuthorization( noRestriction = true )
String getI18nResources( @QueryParam( "locale" ) String locale ) Map<String,String> getI18nResources( @QueryParam( "locale" ) String locale )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
/** /**
@ -60,9 +61,9 @@ public interface CommonServices
*/ */
@Path( "getAllI18nResources" ) @Path( "getAllI18nResources" )
@GET @GET
@Produces( { MediaType.TEXT_PLAIN } ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( noRestriction = true ) @RedbackAuthorization( noRestriction = true )
String getAllI18nResources( @QueryParam( "locale" ) String locale ) Map<String,String> getAllI18nResources( @QueryParam( "locale" ) String locale )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;

View File

@ -26,6 +26,7 @@ import org.apache.archiva.redback.authorization.RedbackAuthorization;
import org.apache.archiva.rest.api.model.ActionStatus; import org.apache.archiva.rest.api.model.ActionStatus;
import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics; import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
import org.apache.archiva.rest.api.model.FileStatus; import org.apache.archiva.rest.api.model.FileStatus;
import org.apache.archiva.rest.api.model.PomSnippet;
import org.apache.archiva.security.common.ArchivaRoleConstants; import org.apache.archiva.security.common.ArchivaRoleConstants;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
@ -116,9 +117,9 @@ public interface ManagedRepositoriesService
*/ */
@Path( "getPomSnippet/{repositoryId}" ) @Path( "getPomSnippet/{repositoryId}" )
@GET @GET
@Produces( { MediaType.TEXT_PLAIN } ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
String getPomSnippet( @PathParam( "repositoryId" ) String repositoryId ) PomSnippet getPomSnippet( @PathParam( "repositoryId" ) String repositoryId )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;

View File

@ -21,6 +21,7 @@ package org.apache.archiva.rest.api.services;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.archiva.redback.authorization.RedbackAuthorization; import org.apache.archiva.redback.authorization.RedbackAuthorization;
import org.apache.archiva.rest.api.model.PingResult;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
@ -45,7 +46,7 @@ public interface PingService
@GET @GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
@RedbackAuthorization( noRestriction = true ) @RedbackAuthorization( noRestriction = true )
String ping(); PingResult ping();
/** /**
* same as #ping but check authz * same as #ping but check authz
@ -56,6 +57,6 @@ public interface PingService
@GET @GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN } )
@RedbackAuthorization( noRestriction = false, noPermission = true ) @RedbackAuthorization( noRestriction = false, noPermission = true )
String pingWithAuthz(); PingResult pingWithAuthz();
} }

View File

@ -22,6 +22,8 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.archiva.redback.authorization.RedbackAuthorization; import org.apache.archiva.redback.authorization.RedbackAuthorization;
import org.apache.archiva.rest.api.model.ActionStatus; import org.apache.archiva.rest.api.model.ActionStatus;
import org.apache.archiva.rest.api.model.CacheEntry; import org.apache.archiva.rest.api.model.CacheEntry;
import org.apache.archiva.rest.api.model.Timestamp;
import org.apache.archiva.rest.api.model.MemoryStatus;
import org.apache.archiva.rest.api.model.QueueEntry; import org.apache.archiva.rest.api.model.QueueEntry;
import org.apache.archiva.rest.api.model.RepositoryScannerStatistics; import org.apache.archiva.rest.api.model.RepositoryScannerStatistics;
import org.apache.archiva.security.common.ArchivaRoleConstants; import org.apache.archiva.security.common.ArchivaRoleConstants;
@ -43,16 +45,16 @@ public interface SystemStatusService
{ {
@Path( "memoryStatus" ) @Path( "memoryStatus" )
@GET @GET
@Produces( MediaType.TEXT_PLAIN ) @Produces( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
String getMemoryStatus() MemoryStatus getMemoryStatus()
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path( "currentServerTime/{locale}" ) @Path( "currentServerTime/{locale}" )
@GET @GET
@Produces( MediaType.TEXT_PLAIN ) @Produces( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION ) @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_MANAGE_CONFIGURATION )
String getCurrentServerTime( @PathParam( "locale" ) String locale ) Timestamp getCurrentServerTime( @PathParam( "locale" ) String locale )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path( "queueEntries" ) @Path( "queueEntries" )

View File

@ -40,6 +40,7 @@ import java.io.InputStream;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/** /**
* @author Olivier Lamy * @author Olivier Lamy
@ -56,7 +57,7 @@ public class DefaultCommonServices
@Inject @Inject
private UtilServices utilServices; private UtilServices utilServices;
private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>(); private Map<String, Map<String,String>> cachei18n = new ConcurrentHashMap<String, Map<String,String>>();
@Inject @Inject
protected CronExpressionValidator cronExpressionValidator; protected CronExpressionValidator cronExpressionValidator;
@ -72,7 +73,7 @@ public class DefaultCommonServices
} }
@Override @Override
public String getI18nResources( String locale ) public Map<String,String> getI18nResources( String locale )
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {
Properties properties = new Properties(); Properties properties = new Properties();
@ -89,7 +90,14 @@ public class DefaultCommonServices
log.warn( "skip error loading properties {}", resourceName ); log.warn( "skip error loading properties {}", resourceName );
} }
return fromProperties( properties ); return properties.entrySet().stream().collect(
Collectors.toMap(
e -> e.getKey().toString(),
e -> e.getValue().toString()
)
);
} }
private void loadResource( Properties properties, StringBuilder resourceName, String locale ) private void loadResource( Properties properties, StringBuilder resourceName, String locale )
@ -142,11 +150,11 @@ public class DefaultCommonServices
} }
@Override @Override
public String getAllI18nResources( String locale ) public Map<String,String> getAllI18nResources( String locale )
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {
String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) ); Map<String,String> cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
if ( cachedi18n != null ) if ( cachedi18n != null )
{ {
return cachedi18n; return cachedi18n;
@ -158,10 +166,14 @@ public class DefaultCommonServices
Properties all = utilServices.getI18nProperties( locale ); Properties all = utilServices.getI18nProperties( locale );
StringBuilder resourceName = new StringBuilder( RESOURCE_NAME ); StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
loadResource( all, resourceName, locale ); loadResource( all, resourceName, locale );
Map<String, String> allMap = all.entrySet().stream().collect(
String i18n = fromProperties( all ); Collectors.toMap(
cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), i18n ); e -> e.getKey().toString(),
return i18n; e -> e.getValue().toString()
)
);
cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), allMap );
return allMap;
} }
catch ( IOException e ) catch ( IOException e )
{ {

View File

@ -30,6 +30,7 @@ import org.apache.archiva.metadata.repository.stats.model.RepositoryStatisticsMa
import org.apache.archiva.rest.api.model.ActionStatus; import org.apache.archiva.rest.api.model.ActionStatus;
import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics; import org.apache.archiva.rest.api.model.ArchivaRepositoryStatistics;
import org.apache.archiva.rest.api.model.FileStatus; import org.apache.archiva.rest.api.model.FileStatus;
import org.apache.archiva.rest.api.model.PomSnippet;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.rest.api.services.ManagedRepositoriesService; import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringEscapeUtils;
@ -211,10 +212,10 @@ public class DefaultManagedRepositoriesService
} }
@Override @Override
public String getPomSnippet( String repositoryId ) public PomSnippet getPomSnippet( String repositoryId )
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {
return createSnippet( getManagedRepository( repositoryId ) ); return new PomSnippet( createSnippet( getManagedRepository( repositoryId ) ) );
} }
private String createSnippet( ManagedRepository repo ) private String createSnippet( ManagedRepository repo )

View File

@ -19,6 +19,7 @@ package org.apache.archiva.rest.services;
* under the License. * under the License.
*/ */
import org.apache.archiva.rest.api.model.PingResult;
import org.apache.archiva.rest.api.services.PingService; import org.apache.archiva.rest.api.services.PingService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -31,13 +32,13 @@ public class DefaultPingService
implements PingService implements PingService
{ {
@Override @Override
public String ping() public PingResult ping()
{ {
return "Yeah Baby It rocks!"; return new PingResult( "Yeah Baby It rocks!" );
} }
@Override @Override
public String pingWithAuthz() public PingResult pingWithAuthz()
{ {
return ping(); return ping();
} }

View File

@ -29,8 +29,10 @@ import org.apache.archiva.repository.scanner.RepositoryScannerInstance;
import org.apache.archiva.rest.api.model.ActionStatus; import org.apache.archiva.rest.api.model.ActionStatus;
import org.apache.archiva.rest.api.model.CacheEntry; import org.apache.archiva.rest.api.model.CacheEntry;
import org.apache.archiva.rest.api.model.ConsumerScanningStatistics; import org.apache.archiva.rest.api.model.ConsumerScanningStatistics;
import org.apache.archiva.rest.api.model.MemoryStatus;
import org.apache.archiva.rest.api.model.QueueEntry; import org.apache.archiva.rest.api.model.QueueEntry;
import org.apache.archiva.rest.api.model.RepositoryScannerStatistics; import org.apache.archiva.rest.api.model.RepositoryScannerStatistics;
import org.apache.archiva.rest.api.model.Timestamp;
import org.apache.archiva.rest.api.services.ArchivaRestServiceException; import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.rest.api.services.SystemStatusService; import org.apache.archiva.rest.api.services.SystemStatusService;
import org.apache.archiva.rest.services.utils.ConsumerScanningStatisticsComparator; import org.apache.archiva.rest.services.utils.ConsumerScanningStatisticsComparator;
@ -86,7 +88,7 @@ public class DefaultSystemStatusService
} }
@Override @Override
public String getMemoryStatus() public MemoryStatus getMemoryStatus()
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
@ -94,7 +96,7 @@ public class DefaultSystemStatusService
long total = runtime.totalMemory(); long total = runtime.totalMemory();
long used = total - runtime.freeMemory(); long used = total - runtime.freeMemory();
long max = runtime.maxMemory(); long max = runtime.maxMemory();
return formatMemory( used ) + "/" + formatMemory( total ) + " (Max: " + formatMemory( max ) + ")"; return new MemoryStatus( formatMemory( used ) + "/" + formatMemory( total ) + " (Max: " + formatMemory( max ) + ")" );
} }
private static String formatMemory( long l ) private static String formatMemory( long l )
@ -103,11 +105,11 @@ public class DefaultSystemStatusService
} }
@Override @Override
public String getCurrentServerTime( String locale ) public Timestamp getCurrentServerTime( String locale )
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {
SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( locale ) ); SimpleDateFormat sdf = new SimpleDateFormat( "EEE, d MMM yyyy HH:mm:ss Z", new Locale( locale ) );
return sdf.format( new Date() ); return new Timestamp( sdf.format( new Date( ) ) );
} }
@Override @Override

View File

@ -19,6 +19,7 @@ package org.apache.archiva.rest.services;
* under the License. * under the License.
*/ */
import org.apache.archiva.rest.api.model.PingResult;
import org.apache.archiva.rest.api.services.PingService; import org.apache.archiva.rest.api.services.PingService;
import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.jaxrs.client.WebClient;
import org.junit.Ignore; import org.junit.Ignore;
@ -42,8 +43,8 @@ public class PingServiceTest
// 1000000L // 1000000L
//WebClient.getConfig( userService ).getHttpConduit().getClient().setReceiveTimeout(3000); //WebClient.getConfig( userService ).getHttpConduit().getClient().setReceiveTimeout(3000);
String res = getPingService().ping(); PingResult res = getPingService().ping();
assertEquals( "Yeah Baby It rocks!", res ); assertEquals( "Yeah Baby It rocks!", res.getOutput() );
} }
@Test( expected = ForbiddenException.class ) @Test( expected = ForbiddenException.class )
@ -53,7 +54,7 @@ public class PingServiceTest
try try
{ {
String res = getPingService().pingWithAuthz(); PingResult res = getPingService().pingWithAuthz();
fail( "not in exception" ); fail( "not in exception" );
} }
catch ( ForbiddenException e ) catch ( ForbiddenException e )
@ -71,8 +72,8 @@ public class PingServiceTest
PingService service = getPingService(); PingService service = getPingService();
WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 ); WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
WebClient.client( service ).header( "Authorization", authorizationHeader ); WebClient.client( service ).header( "Authorization", authorizationHeader );
String res = service.pingWithAuthz(); PingResult res = service.pingWithAuthz();
assertEquals( "Yeah Baby It rocks!", res ); assertEquals( "Yeah Baby It rocks!", res.getOutput() );
} }
@Ignore( "FIXME guest failed ???" ) @Ignore( "FIXME guest failed ???" )
@ -83,7 +84,7 @@ public class PingServiceTest
PingService service = getPingService(); PingService service = getPingService();
WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 ); WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
WebClient.client( service ).header( "Authorization", guestAuthzHeader ); WebClient.client( service ).header( "Authorization", guestAuthzHeader );
String res = service.pingWithAuthz(); PingResult res = service.pingWithAuthz();
assertEquals( "Yeah Baby It rocks!", res ); assertEquals( "Yeah Baby It rocks!", res.getOutput() );
} }
} }