forked from WebGoat/WebGoat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request WebGoat#289 from zupzup/feature/labelservice
Issue WebGoat#265: Created LabelService to support UI localization
- Loading branch information
Showing
4 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...er/src/main/java/org/owasp/webgoat/i18n/ExposedReloadableResourceMessageBundleSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.owasp.webgoat.i18n; | ||
|
||
import org.springframework.context.support.ReloadableResourceBundleMessageSource; | ||
|
||
import java.util.Locale; | ||
import java.util.Properties; | ||
|
||
/** | ||
* <p>ExposedReloadableResourceMessageBundleSource class.</p> | ||
* Extends the reloadable message source with a way to get all messages | ||
* | ||
* @author zupzup | ||
*/ | ||
|
||
public class ExposedReloadableResourceMessageBundleSource extends ReloadableResourceBundleMessageSource { | ||
/** | ||
* Gets all messages for presented Locale. | ||
* @param locale user request's locale | ||
* @return all messages | ||
*/ | ||
public Properties getMessages(Locale locale) { | ||
return getMergedProperties(locale).getProperties(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
webgoat-container/src/main/java/org/owasp/webgoat/service/LabelService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.owasp.webgoat.service; | ||
|
||
import org.owasp.webgoat.i18n.LabelProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
|
||
/** | ||
* <p>LabelService class.</p> | ||
* | ||
* @author zupzup | ||
*/ | ||
|
||
@Controller | ||
public class LabelService { | ||
|
||
private static final String URL_LABELS_MVC = "/service/labels.mvc"; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(LabelService.class); | ||
|
||
@Autowired | ||
private LabelProvider labelProvider; | ||
|
||
/** | ||
* Fetches labels for given language | ||
* If no language is provided, the language is determined from the request headers | ||
* Otherwise, fall back to default language | ||
* | ||
* @param lang the language to fetch labels for (optional) | ||
* @return a map of labels | ||
* @throws Exception | ||
*/ | ||
@RequestMapping(path = URL_LABELS_MVC, produces = MediaType.APPLICATION_JSON_VALUE) | ||
public @ResponseBody | ||
ResponseEntity<Map<String, String>> fetchLabels(@RequestParam(value = "lang", required = false) String lang, HttpServletRequest request) throws Exception { | ||
Locale locale; | ||
if (StringUtils.isEmpty(lang)) { | ||
logger.debug("No language provided, determining from request headers"); | ||
locale = request.getLocale(); | ||
if (locale != null) { | ||
logger.debug("Locale set to {}", locale); | ||
} | ||
} else { | ||
locale = Locale.forLanguageTag(lang); | ||
logger.debug("Language provided: {} leads to Locale: {}", lang, locale); | ||
} | ||
return new ResponseEntity<>(labelProvider.getLabels(locale), HttpStatus.OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters