]> source.dussan.org Git - archiva.git/commitdiff
add rest method to load i18n from redback and archiva in one call
authorOlivier Lamy <olamy@apache.org>
Fri, 6 Jan 2012 08:55:48 +0000 (08:55 +0000)
committerOlivier Lamy <olamy@apache.org>
Fri, 6 Jan 2012 08:55:48 +0000 (08:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1228021 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/CommonServices.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultCommonServices.java
archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/i18nload.js

index c33615a5d1caee7e1abd2b70adbf5498bae34175..28077b03704d7ca52f08c1cb4d188ca2e1bd3f41 100644 (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;
 }
index 75cffe74d18f7505b1363425a16d9f5449a1e414..92f56e11e8ef7804719413c5c848a0e4ec9573cd 100644 (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() );
+        }
+    }
 }
index 15147b171abea3e7be0f08d0ff33e227da90dd42..0eb93543329f432821db925da025e18628b5a8b0 100644 (file)
 
 $(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