Commit 3f3e7979 authored by Matija Obreza's avatar Matija Obreza
Browse files

Fail service creation when Transifex credentials are invalid

parent eab9249c
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.transifex.client;
public class TransifexException extends Exception {
public TransifexException(String message, Throwable e) {
super(message, e);
}
}
......@@ -74,6 +74,24 @@ public class TransifexServiceImpl implements TransifexService, InitializingBean
public void afterPropertiesSet() throws Exception {
transifexProjectUrl = TRANSIFEX_API_URL.concat("/project/").concat(projectSlug);
LOG.info("Transifex project URL: " + transifexProjectUrl);
ensureTransifexCredentials();
}
private void ensureTransifexCredentials() throws TransifexException {
HttpHeaders headers = basicAuthentication();
HttpEntity<String> request = new HttpEntity<>(headers);
try {
ResponseEntity<String> response = template.exchange(transifexProjectUrl + "/?details", HttpMethod.GET, request, String.class);
// response.getBody() // contains JSON information about the project
if (LOG.isInfoEnabled()) {
LOG.info("Transifex connection okay! HTTP status code=" + response.getStatusCode());
}
} catch (HttpClientErrorException e) {
// Fail here to stop initializing the context!
throw new TransifexException("Transifex credentials are not valid.", e);
}
}
// Transifex requires Basic HTTP authentication!
......@@ -200,9 +218,9 @@ public class TransifexServiceImpl implements TransifexService, InitializingBean
// Create Multi value map with all necessary information for request
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
// map.add("slug", slug);
// map.add("name", title);
// map.add("i18n_type", "XHTML");
// map.add("slug", slug);
// map.add("name", title);
// map.add("i18n_type", "XHTML");
File tempFile = File.createTempFile(slug, ".xhtml");
// default FileWriter support default encoding only
......@@ -221,7 +239,7 @@ public class TransifexServiceImpl implements TransifexService, InitializingBean
// Send our post request(with .xhtml file) to Transifex
template.put(transifexProjectUrl + "/resource/{slug}/content/", request, slug);
} catch (HttpClientErrorException e) {
LOG.error(e.getMessage());
LOG.error(e.getResponseBodyAsString());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment