From: Olivier Lamy Date: Fri, 6 Jan 2012 08:55:48 +0000 (+0000) Subject: add rest method to load i18n from redback and archiva in one call X-Git-Tag: archiva-1.4-M3~1637 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bcf944afb63e21ffe187304382136b45c7ee49db;p=archiva.git 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 --- diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/CommonServices.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/CommonServices.java index c33615a5d..28077b037 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/CommonServices.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/CommonServices.java @@ -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; } diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java index 75cffe74d..92f56e11e 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java @@ -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 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 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() ); + } + } } diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js index 15147b171..0eb935433 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js @@ -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'}); }); \ No newline at end of file