add rest method to load i18n from redback and archiva in one call

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1228021 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-01-06 08:55:48 +00:00
parent 293efa4329
commit bcf944afb6
3 changed files with 62 additions and 17 deletions

View File

@ -46,4 +46,17 @@ public interface CommonServices
*/
String getI18nResources( @QueryParam( "locale" ) String locale )
throws ArchivaRestServiceException;
@Path( "getAllI18nResources" )
@GET
@Produces( { MediaType.TEXT_PLAIN } )
@RedbackAuthorization( noRestriction = true )
/**
* will return properties available in org/apache/archiva/i18n/default.properties
* load default (en) then override with locale used so at least en are returned if no
* translation in the locale asked.
* This method will add redback resources too. note Archva wins
*/
String getAllI18nResources( @QueryParam( "locale" ) String locale )
throws ArchivaRestServiceException;
}

View File

@ -22,12 +22,18 @@ import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
import org.apache.archiva.rest.api.services.CommonServices;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.redback.rest.api.services.RedbackServiceException;
import org.codehaus.redback.rest.api.services.UtilServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Map;
import java.util.Properties;
@ -41,6 +47,9 @@ public class DefaultCommonServices
private Logger log = LoggerFactory.getLogger( getClass() );
@Inject
private UtilServices utilServices;
public String getI18nResources( String locale )
throws ArchivaRestServiceException
{
@ -58,15 +67,7 @@ public class DefaultCommonServices
log.warn( "skip error loading properties {}", resourceName.toString() );
}
StringBuilder output = new StringBuilder();
for ( Map.Entry<Object, Object> entry : properties.entrySet() )
{
output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
output.append( '\n' );
}
return output.toString();
return fromProperties( properties );
}
private void loadResource( Properties properties, StringBuilder resourceName, String locale )
@ -83,6 +84,19 @@ public class DefaultCommonServices
}
private String fromProperties( Properties properties )
{
StringBuilder output = new StringBuilder();
for ( Map.Entry<Object, Object> entry : properties.entrySet() )
{
output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
output.append( '\n' );
}
return output.toString();
}
private void loadResource( Properties properties, String resourceName )
throws IOException
{
@ -101,4 +115,27 @@ public class DefaultCommonServices
IOUtils.closeQuietly( is );
}
}
public String getAllI18nResources( String locale )
throws ArchivaRestServiceException
{
try
{
String redbackProps = utilServices.getI18nResources( locale );
String archivaProps = getI18nResources( locale );
Properties properties = new Properties();
properties.load( new StringReader( redbackProps ) );
properties.load( new StringReader( archivaProps ) );
return fromProperties( properties );
}
catch ( RedbackServiceException e )
{
throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode() );
}
catch ( IOException e )
{
throw new ArchivaRestServiceException( e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
}
}
}

View File

@ -19,22 +19,17 @@
$(function() {
// load i18n resources from rest call
// first redback then archiva
// -- redback
// load default
loadAndParseFile("restServices/redbackServices/utilServices/getBundleResources", {cache:false, mode: 'map',encoding:'utf-8'});
// load browser locale
var browserLang = $.i18n.browserLang();
var requestLang = $.urlParam('request_lang');
if (requestLang) {
browserLang=requestLang;
}
$.log("use browserLang:"+browserLang);
loadAndParseFile("restServices/redbackServices/utilServices/getBundleResources?locale="+browserLang, {cache:false, mode: 'map',encoding:'utf-8'});
// -- archiva
// load default
loadAndParseFile("restServices/archivaServices/commonServices/getI18nResources", {cache:false, mode: 'map',encoding:'utf-8'});
loadAndParseFile("restServices/archivaServices/commonServices/getAllI18nResources", {cache:false, mode: 'map',encoding:'utf-8'});
// load browser locale
var browserLang = $.i18n.browserLang();
loadAndParseFile("restServices/archivaServices/commonServices/getI18nResources?locale="+browserLang, {cache:false, mode: 'map',encoding:'utf-8'});
loadAndParseFile("restServices/archivaServices/commonServices/getAllI18nResources?locale="+browserLang, {cache:false, mode: 'map',encoding:'utf-8'});
});