Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.0 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ApplicationTechnicalScope.pdf
Binary file not shown.
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.fluentlenium</groupId>
<artifactId>fluentlenium-assertj</artifactId>
<version>0.10.3</version>
</dependency>
<dependency>
<groupId>com.github.detro</groupId>
<artifactId>phantomjsdriver</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.unbabel.challenge.configuration;

import com.unbabel.challenge.configuration.beans.ApiRequestBuilderUtil;
import com.unbabel.challenge.configuration.beans.SessionAttributes;
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;

@Configuration
public class UnbabelBeansConfig
{

@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}

@Bean
public ApiRequestBuilderUtil getEndpointUrlBuilderUtil(){return new ApiRequestBuilderUtil();}

@Bean
public ModelMapper modelMapper() {return new ModelMapper();}

@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public SessionAttributes SessionAttributes() {
return new SessionAttributes();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.unbabel.challenge.configuration;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("unbabel.endpoint")
@Setter @Getter
public class UnbabelEndpointProperties
{
private String baseUrl;
private String userNameParamKey;
private String apiKeyParamKey;
private String userNameParamValue;
private String apiKeyParamValue;
private String languagePairApiUrl;
private String translationApiUrl;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.unbabel.challenge.configuration.beans;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.unbabel.challenge.configuration.UnbabelEndpointProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Arrays;

@Slf4j
public class ApiRequestBuilderUtil implements UnbabelApiBuild
{
private final static String AUTH_HEADER_SUFIX = "ApiKey";
private final static String AUTH_HEADER_NAME = "Authorization";
private static final String SLASH = "/";

@Autowired
UnbabelEndpointProperties endpointProperties;


@Override
public String buildLanguagueApiUrl(){
return buildEndpointUrl(endpointProperties.getLanguagePairApiUrl()).toUriString();
}

@Override
public String buildTranslationApiUrl(){
return buildEndpointUrl(endpointProperties.getTranslationApiUrl()).toUriString();
}

@Override
public String addParameterToUrl(final String url, final String parameter){
return url + parameter;
}

@Override
public HttpHeaders buildBaseHttpHeader(){

HttpHeaders requestHeaders = buildAuthorizattionHttpHeader();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

return requestHeaders;
}

private HttpHeaders buildAuthorizattionHttpHeader(){
String authorizationHeader = AUTH_HEADER_SUFIX + " "
+ endpointProperties.getUserNameParamValue() + ":"
+ endpointProperties.getApiKeyParamValue();

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add(AUTH_HEADER_NAME, authorizationHeader);

return requestHeaders;
}

protected UriComponentsBuilder buildEndpointUrl(final String apiUrl){
return UriComponentsBuilder.fromUriString(endpointProperties.getBaseUrl() + apiUrl);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.unbabel.challenge.configuration.beans;

import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;

@Getter @Setter
public class SessionAttributes
{
private HashMap<String, Object> attributes = new HashMap<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.unbabel.challenge.configuration.beans;

import org.springframework.http.HttpHeaders;

public interface UnbabelApiBuild
{
/**
* Builds url for Unbabel language api
* @return url for unbabel language api
*/
String buildLanguagueApiUrl();

/**
* Builds url for Unbabel translation api
* @return url for unbabel translation api
*/
String buildTranslationApiUrl();

/**
* Builds Http Header with authorization and media information to consume Unbabel api
* @return HttpHeader with authorization and media information
*/
HttpHeaders buildBaseHttpHeader();

/**
* Adds a parameter to a given url
* @param url base url to be modified
* @param parameter string to add to the base url
* @return a url string with the provided url and parameter
*/
String addParameterToUrl(final String url, final String parameter);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.unbabel.challenge.controller;

import com.unbabel.challenge.facade.data.TranslateFormData;
import com.unbabel.challenge.facade.UnbabelTranslationFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;

/**
* Main controller for home page
*/
@Controller
public class UnbabelTranslationController
{
private static final String TRANSLATION_ENTRY = "translationEntryRow";
private static final String FRAG_TARGET_LANGUAGE = "fragments/targetLanguages :: targetLanguagesSelector";
private static final String FRAG_TRANSLATION_ENTRY = "fragments/newEntryRow :: translationEntry";
private static final String TARGET_LANGUAGES = "targetLanguages";
private static final String AVAILABLE_SOURCELANGUAGES = "availableSourcelanguagues";
private static final String TRANSLATIONS_ENTRIES = "translationsEntries";
private static final String INDEX_PAGE = "index";

@Autowired
private UnbabelTranslationFacade translationFacade;

/**
* Accepts get requests to the "/" url, generates random messages
* and renders them in thymeleaf template (index.html).
* @param model inject objects into thymeleaf template
* @return generated html home page
*/
@GetMapping("/")
public String main(Model model) {
model.addAttribute(AVAILABLE_SOURCELANGUAGES, translationFacade.getAvailableSourceLanguages());
model.addAttribute(TRANSLATIONS_ENTRIES, translationFacade.getAllTranslations());

return INDEX_PAGE;
}

/**
* Accepts get request to the url "/targetlanguages/{language}" with the source language code to return
* the compatible target languages
* @param model inject objects into thymeleaf template
* @param languageCode selected source language code
* @return generated html fragment to display target languages
*/
@RequestMapping(value= "/targetlanguages/{language}", method = RequestMethod.GET)
public String getCompatibleTargetLanguages(Model model, @PathVariable("language") String languageCode){
model.addAttribute(TARGET_LANGUAGES, translationFacade.getCompatibleTargetLanguagues(languageCode));

return FRAG_TARGET_LANGUAGE;
}

/**
* Accepts post request to the url "/translate" to create a translation into Unbabel service
* @param model inject objects into thymeleaf template
* @param formData data in json format with parameters to create a translation
* @return generated html fragment with the information about the created translation
*/
@PostMapping("/translate")
public String processForm(Model model,@Valid @RequestBody TranslateFormData formData) {
model.addAttribute(TRANSLATION_ENTRY, translationFacade.submitTranslation(formData));

return FRAG_TRANSLATION_ENTRY;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.unbabel.challenge.dto.languages;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class AvailableLanguagesDTO
{
@JsonProperty("objects")
private List<LanguageDTO> languages;
}
13 changes: 13 additions & 0 deletions src/main/java/com/unbabel/challenge/dto/languages/LanguageDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.unbabel.challenge.dto.languages;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class LanguageDTO
{
@JsonProperty("lang_pair")
private LanguagePairDTO languagePairDTO;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.unbabel.challenge.dto.languages;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter @Setter
public class LanguagePairDTO
{
@JsonProperty("source_language")
private SourceLanguageDTO sourceLanguage;

@JsonProperty("target_language")
private TargetLanguageDTO targetLanguage;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.unbabel.challenge.dto.languages;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class SourceLanguageDTO
{
@JsonProperty("name")
private String name;

@JsonProperty("shortname")
private String shortname;
}
Loading