Skip to content

Commit

Permalink
[CST-2877] fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ddinuzzo committed Jun 23, 2020
1 parent 2016805 commit 8407a2f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 44 deletions.
22 changes: 0 additions & 22 deletions dspace-api/src/main/java/org/dspace/core/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ public class Context implements AutoCloseable {
*/
private Locale currentLocale;

/**
* List of locale object indicating the locales that are
* acceptable to the client based on the Accept-Language header.
* The Locales are in decreasing order starting with the preferred locale.
*/
private List<Locale> clientLocales;

/**
* Extra log info
*/
Expand Down Expand Up @@ -884,19 +877,4 @@ private void reloadContextBoundEntities() throws SQLException {
currentUser = reloadEntity(currentUser);
}

/**
* Returns a list of locale object indicating the locales that are
* acceptable to the client based on the Accept-Language header.
* The Locales are in decreasing order starting with the preferred locale.
* @return List<Locale>
*/
public List<Locale> getClientLocales() {
return clientLocales;
}


public void setClientLocales(List<Locale> clientLocales) {
this.clientLocales = clientLocales;
}

}
19 changes: 19 additions & 0 deletions dspace-api/src/main/java/org/dspace/core/I18nUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,23 @@ public static Locale[] parseLocales(String[] locales) {
}
return resultList.toArray(new Locale[resultList.size()]);
}

/**
* Check if the input locale is in the list of supported locales
* @param locale
* @return true if locale is supported, false otherwise
*/
public static boolean isSupportedLocale(Locale locale) {
boolean isSupported = false;
Locale[] supportedLocales = getSupportedLocales();
if (supportedLocales != null) {
for (Locale sLocale: supportedLocales) {
if (locale.getLanguage().equals(sLocale.getLanguage()) ) {
isSupported = true;
break;
}
}
}
return isSupported;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
*/
package org.dspace.app.rest.repository;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;

import org.dspace.app.rest.converter.ConverterService;
Expand Down Expand Up @@ -44,7 +42,6 @@ protected Context obtainContext() {
context = ContextUtil.obtainContext(currentRequest.getServletRequest());
Locale currentLocale = getLocal(context, currentRequest);
context.setCurrentLocale(currentLocale);
context.setClientLocales(getLocalesFromRequest(currentRequest));
return context;
}

Expand All @@ -61,9 +58,16 @@ private Locale getLocal(Context context, Request request) {
userLocale = new Locale(userLanguage);
}
}
// Locales requested from client
Enumeration<Locale> locales = request.getHttpServletRequest().getLocales();
if (locales != null) {
userLocale = locales.nextElement();
while (locales.hasMoreElements()) {
Locale current = locales.nextElement();
if (I18nUtil.isSupportedLocale(current)) {
userLocale = current;
break;
}
}
}
if (userLocale == null) {
return I18nUtil.getDefaultLocale();
Expand All @@ -72,14 +76,4 @@ private Locale getLocal(Context context, Request request) {
return supportedLocale;
}

private List<Locale> getLocalesFromRequest(Request request) {
List<Locale> locales = new ArrayList<>();
Enumeration<Locale> reqLocales = request.getHttpServletRequest().getLocales();
if (reqLocales != null) {
while (reqLocales.hasMoreElements()) {
locales.add(reqLocales.nextElement());
}
}
return locales;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,8 @@ public SubmissionFormRestRepository() throws DCInputsReaderException {
@Override
public SubmissionFormRest findOne(Context context, String submitName) {
try {
List<Locale> clientLocales = context.getClientLocales();
DCInputsReader inputReader = null;
for (Locale locale: clientLocales) {
inputReader = inputReaders.get(locale);
if (inputReader != null) {
break;
}
}
Locale currentLocale = context.getCurrentLocale();
DCInputsReader inputReader = inputReaders.get(currentLocale);
if (inputReader == null) {
inputReader = defaultInputReader;
}
Expand Down

0 comments on commit 8407a2f

Please sign in to comment.