View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.impl.action.application.translation;
20  
21  import java.io.IOException;
22  import java.security.GeneralSecurityException;
23  import java.util.Arrays;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.stereotype.Service;
30  import org.springframework.transaction.annotation.Propagation;
31  import org.springframework.transaction.annotation.Transactional;
32  
33  import com.google.api.client.json.jackson2.JacksonFactory;
34  import com.google.api.services.translate.Translate;
35  import com.google.api.services.translate.model.TranslationsListResponse;
36  import com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration;
37  import com.hack23.cia.model.internal.application.system.impl.ConfigurationGroup;
38  import com.hack23.cia.service.data.api.ApplicationConfigurationService;
39  
40  /**
41   * The Class TranslationServiceImpl.
42   */
43  @Service
44  @Transactional(propagation = Propagation.REQUIRED)
45  final class TranslationServiceImpl implements TranslationService {
46  
47  	/** The Constant LOGGER. */
48  	private static final Logger LOGGER = LoggerFactory.getLogger(TranslationServiceImpl.class);
49  
50  	/** The application configuration service. */
51  	@Autowired
52  	private ApplicationConfigurationService applicationConfigurationService;
53  
54  	/**
55  	 * Instantiates a new translation service impl.
56  	 */
57  	public TranslationServiceImpl() {
58  		super();
59  	}
60  
61  	@Override
62  	public String translate(final String translateText, final String targetLanguage) throws TranslationException {
63  
64  		final ApplicationConfiguration googleTranslateApiKey = applicationConfigurationService.checkValueOrLoadDefault(
65  				"Google translate api accesskey", "Required for use of the translation service",
66  				ConfigurationGroup.AUTHENTICATION, TranslationServiceImpl.class.getSimpleName(), "Translation Service",
67  				"Responsible configure access credentials for google translate api",
68  				"external.services.google.translate.api.accesskey", "");
69  		final ApplicationConfiguration googleTranslateApiApplicationName = applicationConfigurationService
70  				.checkValueOrLoadDefault("Google translate api application name",
71  						"Required for use of the translation service", ConfigurationGroup.AUTHENTICATION,
72  						TranslationServiceImpl.class.getSimpleName(), "Translation Service",
73  						"Responsible configure application name for google translate api",
74  						"external.services.google.translate.api.application.name", "");
75  
76  		if (!StringUtils.isBlank(googleTranslateApiKey.getPropertyValue())
77  				&& !StringUtils.isBlank(googleTranslateApiApplicationName.getPropertyValue())) {
78  
79  			try {
80  				final Translate t = new Translate.Builder(
81  						com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(),
82  						JacksonFactory.getDefaultInstance(), null)
83  								.setApplicationName(googleTranslateApiApplicationName.getPropertyValue()).build();
84  
85  				final Translate.Translations.List list = t.new Translations().list(Arrays.asList(translateText),
86  						targetLanguage);
87  				list.setKey(googleTranslateApiKey.getPropertyValue());
88  				list.setSource("EN");
89  				final TranslationsListResponse response = list.execute();
90  
91  				return response.getTranslations().listIterator().next().getTranslatedText();
92  
93  			} catch (GeneralSecurityException | IOException e) {
94  				final StringBuilder stringBuilder = new StringBuilder().append("Problem translation text:")
95  						.append(translateText).append("to language:").append(targetLanguage);
96  				LOGGER.warn(stringBuilder.toString(), e);
97  				throw new TranslationException(stringBuilder.toString(), e);
98  			}
99  		}
100 		throw new TranslationException("Missing google api key or application name", null);
101 	}
102 }