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.components.scheduler.CronExpressionValidator;
27 import org.codehaus.redback.rest.api.services.RedbackServiceException;
28 import org.codehaus.redback.rest.api.services.UtilServices;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Inject;
34 import javax.ws.rs.QueryParam;
35 import javax.ws.rs.core.Response;
36 import java.io.ByteArrayInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
40 import java.util.Properties;
43 * @author Olivier Lamy
45 @Service( "commonServices#rest" )
46 public class DefaultCommonServices
47 implements CommonServices
50 private static final String RESOURCE_NAME = "org/apache/archiva/i18n/default";
52 private Logger log = LoggerFactory.getLogger( getClass() );
55 private UtilServices utilServices;
58 protected CronExpressionValidator cronExpressionValidator;
60 public String getI18nResources( String locale )
61 throws ArchivaRestServiceException
63 Properties properties = new Properties();
65 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
69 loadResource( properties, resourceName, locale );
72 catch ( IOException e )
74 log.warn( "skip error loading properties {}", resourceName.toString() );
77 return fromProperties( properties );
80 private void loadResource( Properties properties, StringBuilder resourceName, String locale )
84 loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString(), locale );
85 // if locale override with locale content
86 if ( StringUtils.isNotEmpty( locale ) )
88 loadResource( properties,
89 new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString(),
95 private String fromProperties( final Properties properties )
97 StringBuilder output = new StringBuilder();
99 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
101 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
102 output.append( '\n' );
105 return output.toString();
108 private void loadResource( final Properties finalProperties, String resourceName, String locale )
111 InputStream is = null;
112 Properties properties = new Properties();
115 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
118 properties.load( is );
119 finalProperties.putAll( properties );
123 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
125 log.info( "cannot load resource {}", resourceName );
131 IOUtils.closeQuietly( is );
135 public String getAllI18nResources( String locale )
136 throws ArchivaRestServiceException
141 Properties all = utilServices.getI18nProperties( locale );
142 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
143 loadResource( all, resourceName, locale );
145 return fromProperties( all );
147 catch ( IOException e )
149 throw new ArchivaRestServiceException( e.getMessage(),
150 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
152 catch ( RedbackServiceException e )
154 throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode() );
158 private void loadFromString( String propsStr, Properties properties )
159 throws ArchivaRestServiceException
161 InputStream inputStream = null;
164 inputStream = new ByteArrayInputStream( propsStr.getBytes() );
165 properties.load( inputStream );
167 catch ( IOException e )
169 throw new ArchivaRestServiceException( e.getMessage(),
170 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
174 IOUtils.closeQuietly( inputStream );
178 public ArchivaRuntimeInfo archivaRuntimeInfo()
180 return new ArchivaRuntimeInfo();
183 public Boolean validateCronExpression( String cronExpression )
184 throws ArchivaRestServiceException
186 return cronExpressionValidator.validate( cronExpression );