1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.rest.api.model.ArchivaRuntimeInfo;
22 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
23 import org.apache.archiva.rest.api.services.CommonServices;
24 import org.apache.commons.io.IOUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.codehaus.redback.rest.api.services.RedbackServiceException;
27 import org.codehaus.redback.rest.api.services.UtilServices;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.stereotype.Service;
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;
38 import java.util.Properties;
41 * @author Olivier Lamy
43 @Service( "commonServices#rest" )
44 public class DefaultCommonServices
45 implements CommonServices
48 private static final String RESOURCE_NAME = "org/apache/archiva/i18n/default";
50 private Logger log = LoggerFactory.getLogger( getClass() );
53 private UtilServices utilServices;
55 public String getI18nResources( String locale )
56 throws ArchivaRestServiceException
58 Properties properties = new Properties();
60 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
64 loadResource( properties, resourceName, locale );
67 catch ( IOException e )
69 log.warn( "skip error loading properties {}", resourceName.toString() );
72 return fromProperties( properties );
75 private void loadResource( Properties properties, StringBuilder resourceName, String locale )
79 loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString(), locale );
80 // if locale override with locale content
81 if ( StringUtils.isNotEmpty( locale ) )
83 loadResource( properties,
84 new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString(),
90 private String fromProperties( final Properties properties )
92 StringBuilder output = new StringBuilder();
94 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
96 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
97 output.append( '\n' );
100 return output.toString();
103 private void loadResource( final Properties finalProperties, String resourceName, String locale )
106 InputStream is = null;
107 Properties properties = new Properties();
110 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
113 properties.load( is );
114 finalProperties.putAll( properties );
118 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
120 log.info( "cannot load resource {}", resourceName );
126 IOUtils.closeQuietly( is );
130 public String getAllI18nResources( String locale )
131 throws ArchivaRestServiceException
136 Properties all = utilServices.getI18nProperties( locale );
137 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
138 loadResource( all, resourceName, locale );
140 return fromProperties( all );
142 catch ( IOException e )
144 throw new ArchivaRestServiceException( e.getMessage(),
145 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
147 catch ( RedbackServiceException e )
149 throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode() );
153 private void loadFromString( String propsStr, Properties properties )
154 throws ArchivaRestServiceException
156 InputStream inputStream = null;
159 inputStream = new ByteArrayInputStream( propsStr.getBytes() );
160 properties.load( inputStream );
162 catch ( IOException e )
164 throw new ArchivaRestServiceException( e.getMessage(),
165 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
169 IOUtils.closeQuietly( inputStream );
173 public ArchivaRuntimeInfo archivaRuntimeInfo()
175 return new ArchivaRuntimeInfo();