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

Sidm 4120 welsh post requests #370

Merged
5 commits merged into from
May 18, 2020
Merged
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
13 changes: 10 additions & 3 deletions src/main/java/uk/gov/hmcts/reform/idam/web/helper/JSPHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.hmcts.reform.idam.web.helper;

import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
Expand All @@ -13,6 +14,8 @@

import javax.annotation.Nonnull;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Locale;
import java.util.Optional;

Expand All @@ -28,14 +31,16 @@ public class JSPHelper {
* @should throw if there is no request in context
*/
@Nonnull
public static String getOtherLocaleUrl() {
@SneakyThrows(UnsupportedEncodingException.class)
public String getOtherLocaleUrl() {
final ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
final HttpServletRequest request = servletRequestAttributes != null ? servletRequestAttributes.getRequest() : null;
final String targetLocale = getTargetLocale();

if (request != null) {
final String requestUri = PATH_HELPER.getOriginatingRequestUri(request);
final String requestQueryString = PATH_HELPER.getOriginatingQueryString(request);
final String originatingQueryString = PATH_HELPER.getOriginatingQueryString(request);
final String requestQueryString = originatingQueryString == null ? null : URLDecoder.decode(originatingQueryString, "UTF-8"); //NOSONAR
final UriComponentsBuilder initialUrl = UriComponentsBuilder.fromPath(requestUri).replaceQuery(requestQueryString);

return overrideLocaleParameter(initialUrl, targetLocale);
Expand All @@ -44,12 +49,14 @@ public static String getOtherLocaleUrl() {
}

/**
* @param builder the URI builder
* @param builder the URI builder
* @param targetLocale the target locale
*
* @should override existing parameter
* @should add nonexisting parameter
*/
public static String overrideLocaleParameter(@NonNull final UriComponentsBuilder builder, @NonNull final String targetLocale) {

return builder.replaceQueryParam(MessagesConfiguration.UI_LOCALES_PARAM_NAME, new Locale(targetLocale)).toUriString();
}

Expand Down