Java tutorial
/** * Copyright (C) 2015 Agro-Know, Deutsches Forschungszentrum fr Knstliche Intelligenz, iMinds, * Institut fr Angewandte Informatik e. V. an der Universitt Leipzig, * Istituto Superiore Mario Boella, Tilde, Vistatec, WRIPL (http://freme-project.eu) * * 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 eu.freme.broker.eservices; import com.google.common.base.Strings; import com.mashape.unirest.request.HttpRequestWithBody; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; import eu.freme.broker.exception.BadRequestException; import eu.freme.broker.exception.ExternalServiceFailedException; import eu.freme.broker.tools.NIFParameterSet; import eu.freme.common.conversion.etranslate.TranslationConversionService; import eu.freme.common.conversion.rdf.RDFConstants; import eu.freme.common.conversion.rdf.RDFConstants.RDFSerialization; /** * REST controller for Tilde e-Translation service * * @author Jan Nehring - jan.nehring@dfki.de */ @RestController @Profile("broker") public class TildeETranslation extends BaseRestController { @Autowired TranslationConversionService translationConversionService; @Value("${freme.broker.tildeETranslationUrl:https://services.tilde.com/translation/?sourceLang={source-lang}&targetLang={target-lang}}") private String endpoint;// = "https://services.tilde.com/translation/?sourceLang={source-lang}&targetLang={target-lang}"; @RequestMapping(value = "/e-translation/tilde", method = RequestMethod.POST) public ResponseEntity<String> tildeTranslate(@RequestParam(value = "input", required = false) String input, @RequestParam(value = "i", required = false) String i, @RequestParam(value = "informat", required = false) String informat, @RequestParam(value = "f", required = false) String f, @RequestParam(value = "outformat", required = false) String outformat, @RequestParam(value = "o", required = false) String o, @RequestParam(value = "prefix", required = false) String prefix, @RequestParam(value = "p", required = false) String p, @RequestHeader(value = "Accept", required = false) String acceptHeader, @RequestHeader(value = "Content-Type", required = false) String contentTypeHeader, @RequestBody(required = false) String postBody, @RequestParam(value = "source-lang") String sourceLang, @RequestParam(value = "target-lang") String targetLang, @RequestParam(value = "domain", defaultValue = "") String domain, @RequestParam(value = "system", defaultValue = "full") String system, @RequestParam(value = "key", required = false) String key) { // merge long and short parameters - long parameters override short // parameters if (input == null) { input = i; } if (informat == null) { informat = f; } if (outformat == null) { outformat = o; } if (prefix == null) { prefix = p; } NIFParameterSet parameters = this.normalizeNif(input, informat, outformat, postBody, acceptHeader, contentTypeHeader, prefix); // create rdf model String plaintext = null; Model inputModel = ModelFactory.createDefaultModel(); if (!parameters.getInformat().equals(RDFConstants.RDFSerialization.PLAINTEXT)) { // input is nif try { inputModel = this.unserializeNif(parameters.getInput(), parameters.getInformat()); } catch (Exception e) { logger.error("failed", e); throw new BadRequestException("Error parsing NIF input"); } } else { // input is plaintext plaintext = parameters.getInput(); rdfConversionService.plaintextToRDF(inputModel, plaintext, sourceLang, parameters.getPrefix()); } // send request to tilde mt Model responseModel = null; try { HttpResponse<String> response = Unirest.post(endpoint).routeParam("source-lang", sourceLang) .routeParam("target-lang", targetLang).header("Accept", "application/x-turtle") .header("Content-Type", "application/x-turtle").queryString("system", system) .header("Authentication", "Basic RlJFTUU6dXxGcjNtM19zJGN1ciQ=").queryString("domain", domain) .queryString("key", key) .body(rdfConversionService.serializeRDF(inputModel, RDFSerialization.TURTLE)).asString(); if (response.getStatus() != HttpStatus.OK.value()) { throw new ExternalServiceFailedException( "External service failed with status code " + response.getStatus(), HttpStatus.valueOf(response.getStatus())); } String translation = response.getBody(); responseModel = rdfConversionService.unserializeRDF(translation, RDFSerialization.TURTLE); } catch (Exception e) { if (e instanceof ExternalServiceFailedException) { throw new ExternalServiceFailedException(e.getMessage(), ((ExternalServiceFailedException) e).getHttpStatusCode()); } else { throw new ExternalServiceFailedException(e.getMessage()); } } return createSuccessResponse(responseModel, parameters.getOutformat()); } }