This project has retired. For details please refer to its Attic page.
DefaultCommonServices xref
View Javadoc
1   package org.apache.archiva.rest.services;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.redback.components.scheduler.CronExpressionValidator;
22  import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
23  import org.apache.archiva.redback.rest.api.services.UtilServices;
24  import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
25  import org.apache.archiva.rest.api.services.CommonServices;
26  import org.apache.commons.lang.StringUtils;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  import org.springframework.stereotype.Service;
30  
31  import javax.annotation.PostConstruct;
32  import javax.inject.Inject;
33  import javax.ws.rs.core.Response;
34  import java.io.ByteArrayInputStream;
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.util.Map;
38  import java.util.Properties;
39  import java.util.concurrent.ConcurrentHashMap;
40  
41  /**
42   * @author Olivier Lamy
43   */
44  @Service("commonServices#rest")
45  public class DefaultCommonServices
46      implements CommonServices
47  {
48  
49      private static final String RESOURCE_NAME = "org/apache/archiva/i18n/default";
50  
51      private Logger log = LoggerFactory.getLogger( getClass() );
52  
53      @Inject
54      private UtilServices utilServices;
55  
56      private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>();
57  
58      @Inject
59      protected CronExpressionValidator cronExpressionValidator;
60  
61      @PostConstruct
62      public void init()
63          throws ArchivaRestServiceException
64      {
65  
66          // preload i18n en and fr
67          getAllI18nResources( "en" );
68          getAllI18nResources( "fr" );
69      }
70  
71      @Override
72      public String getI18nResources( String locale )
73          throws ArchivaRestServiceException
74      {
75          Properties properties = new Properties();
76  
77          StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
78          try
79          {
80  
81              loadResource( properties, resourceName, locale );
82  
83          }
84          catch ( IOException e )
85          {
86              log.warn( "skip error loading properties {}", resourceName.toString() );
87          }
88  
89          return fromProperties( properties );
90      }
91  
92      private void loadResource( Properties properties, StringBuilder resourceName, String locale )
93          throws IOException
94      {
95          // load default
96          loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString(), locale );
97          // if locale override with locale content
98          if ( StringUtils.isNotEmpty( locale ) )
99          {
100             loadResource( properties,
101                           new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString(),
102                           locale );
103         }
104 
105     }
106 
107     private String fromProperties( final Properties properties )
108     {
109         StringBuilder output = new StringBuilder();
110 
111         for ( Map.Entry<Object, Object> entry : properties.entrySet() )
112         {
113             output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
114             output.append( '\n' );
115         }
116 
117         return output.toString();
118     }
119 
120     private void loadResource( final Properties finalProperties, String resourceName, String locale )
121         throws IOException
122     {
123         Properties properties = new Properties();
124         try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName ))
125         {
126             if ( is != null )
127             {
128                 properties.load( is );
129                 finalProperties.putAll( properties );
130             }
131             else
132             {
133                 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
134                 {
135                     log.info( "cannot load resource {}", resourceName );
136                 }
137             }
138         }
139     }
140 
141     @Override
142     public String getAllI18nResources( String locale )
143         throws ArchivaRestServiceException
144     {
145 
146         String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
147         if ( cachedi18n != null )
148         {
149             return cachedi18n;
150         }
151 
152         try
153         {
154 
155             Properties all = utilServices.getI18nProperties( locale );
156             StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
157             loadResource( all, resourceName, locale );
158 
159             String i18n = fromProperties( all );
160             cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), i18n );
161             return i18n;
162         }
163         catch ( IOException e )
164         {
165             throw new ArchivaRestServiceException( e.getMessage(),
166                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
167         }
168         catch ( RedbackServiceException e )
169         {
170             throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode(), e );
171         }
172     }
173 
174     private void loadFromString( String propsStr, Properties properties )
175         throws ArchivaRestServiceException
176     {
177         try (InputStream inputStream = new ByteArrayInputStream( propsStr.getBytes() ))
178         {
179             properties.load( inputStream );
180         }
181         catch ( IOException e )
182         {
183             throw new ArchivaRestServiceException( e.getMessage(),
184                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
185         }
186     }
187 
188 
189     @Override
190     public Boolean validateCronExpression( String cronExpression )
191         throws ArchivaRestServiceException
192     {
193         return cronExpressionValidator.validate( cronExpression );
194     }
195 }