diff --git a/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java b/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java index a6b5c7cb2..38eb05edd 100644 --- a/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java +++ b/omod-1.10/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_10/PersonResource1_10Test.java @@ -35,7 +35,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Mr. Horatio Test Hornblower Esq."; + return "Horatio Test Hornblower"; } @Override diff --git a/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java b/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java index bfe6d999e..f54fad8c5 100644 --- a/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java +++ b/omod-1.11/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_11/PersonResource1_11Test.java @@ -37,7 +37,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Mr. Horatio Test Hornblower"; + return "Horatio Test Hornblower"; } @Override diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java index 15de940cc..261ed9167 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8.java @@ -9,6 +9,7 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8; +import java.lang.reflect.Method; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -28,6 +29,8 @@ import org.openmrs.PersonAttribute; import org.openmrs.PersonName; import org.openmrs.api.context.Context; +import org.openmrs.layout.web.name.NameSupport; +import org.openmrs.layout.web.name.NameTemplate; import org.openmrs.module.webservices.rest.SimpleObject; import org.openmrs.module.webservices.rest.web.ConversionUtil; import org.openmrs.module.webservices.rest.web.RequestContext; @@ -465,8 +468,22 @@ public String getDisplayString(Person person) { // TODO copy what is done in PatientResource to use configured name layout if (person.getPersonName() == null) return ""; - - return person.getPersonName().getFullName(); + + PersonName personName = person.getPersonName(); + try { + NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate(); + + if (nameTemplate != null) { + // need to use reflection since the format method was not added until later versions of openmrs + Method format = NameTemplate.class.getDeclaredMethod("format", PersonName.class); + return (String) format.invoke(nameTemplate, personName); + } + } + catch (Exception e) { + // fall through to just returning full name if no format method found or format fails + } + + return personName.getFullName(); } private static void copyNameFields(PersonName existingName, PersonName personName) { diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java index 9a8f6a71d..6a8400c10 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PatientResource1_8Test.java @@ -61,7 +61,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Mr. Horatio Test Hornblower Esq."; + return "Horatio Test Hornblower"; } @Override diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java index e00c3a0e4..051fee9b2 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/PersonResource1_8Test.java @@ -70,7 +70,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Mr. Horatio Test Hornblower Esq."; + return "Horatio Test Hornblower"; } @Override diff --git a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java new file mode 100644 index 000000000..c199c228f --- /dev/null +++ b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java @@ -0,0 +1,157 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9; + +import org.apache.commons.lang3.LocaleUtils; +import org.openmrs.Location; +import org.openmrs.Provider; +import org.openmrs.User; +import org.openmrs.api.APIException; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.ConversionUtil; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.api.RestService; +import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation; +import org.openmrs.module.webservices.rest.web.representation.Representation; +import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController; +import org.openmrs.util.PrivilegeConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; +import java.util.Collection; +import java.util.HashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +/** + * Controller that lets a client check the status of their session, and log out. (Authenticating is + * handled through a filter, and may happen through this or any other resource. + */ +@Controller +@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/session") +public class SessionController1_9 extends BaseRestController { + + private static final Logger log = LoggerFactory.getLogger(SessionController1_9.class); + + public static final String USER_CUSTOM_REP = "(uuid,display,username,systemId,userProperties,person:(uuid,display),privileges:(uuid,display,name),roles:(uuid,display,name),links)"; + + @Autowired + RestService restService; + + /** + * Tells the user whether they are authenticated and provides details on the logged-in user + */ + @RequestMapping(method = RequestMethod.GET) + @ResponseBody + public Object get() { + boolean authenticated = Context.isAuthenticated(); + SimpleObject session = new SimpleObject(); + session.add("authenticated", authenticated); + if (authenticated) { + session.add("user", ConversionUtil.convertToRepresentation(Context.getAuthenticatedUser(), + new CustomRepresentation(USER_CUSTOM_REP))); + session.add("locale", Context.getLocale()); + session.add("allowedLocales", Context.getAdministrationService().getAllowedLocales()); + session.add("sessionLocation", ConversionUtil.convertToRepresentation(Context.getUserContext().getLocation(), Representation.REF)); + session.add("currentProvider", ConversionUtil.convertToRepresentation(getCurrentProvider(), Representation.REF)); + } + return session; + } + + @RequestMapping(method = RequestMethod.POST) + @ResponseBody + @ResponseStatus(value = HttpStatus.OK) + public Object post(HttpServletRequest request, @RequestBody Map body) { + String localeStr = body.get("locale"); + if (localeStr != null) { + Locale locale = null; + try { + locale = LocaleUtils.toLocale(localeStr); + } + catch (IllegalArgumentException e) { + throw new APIException(" '" + localeStr + "' does not represent a valid locale."); + } + Set allowedLocales = new HashSet(Context.getAdministrationService().getAllowedLocales()); + if (allowedLocales.contains(locale)) { + Context.setLocale(locale); + } else { + throw new APIException(" '" + localeStr + "' is not in the list of allowed locales."); + } + } + String locationUuid = body.get("sessionLocation"); + if (locationUuid != null) { + Location location = Context.getLocationService().getLocationByUuid(locationUuid); + if (location == null) { + throw new APIException(" '" + locationUuid + "' is not the UUID of any location."); + } + Context.getUserContext().setLocation(location); + { // for compatability with AppUi session location + request.getSession().setAttribute("emrContext.sessionLocationId", location.getId()); + } + } + return get(); + } + + /** + * Logs the client out + * + * Should log the client out + */ + @RequestMapping(method = RequestMethod.DELETE) + @ResponseBody + @ResponseStatus(value = HttpStatus.NO_CONTENT) + public void delete(HttpServletRequest request) { + Context.logout(); + HttpSession session = request.getSession(false); + if (session != null && request.isRequestedSessionIdValid()) { + session.invalidate(); + } + } + + /** + * Get current provider + * + * @return Provider if the user is authenticated + */ + private Provider getCurrentProvider() { + Provider currentProvider = null; + User currentUser = Context.getAuthenticatedUser(); + if (currentUser != null) { + Collection providers = new HashSet(); + try { + Context.addProxyPrivilege(PrivilegeConstants.VIEW_PROVIDERS); + if (currentUser.getPerson() != null) { + providers = Context.getProviderService().getProvidersByPerson(currentUser.getPerson(), false); + } + } + finally { + Context.removeProxyPrivilege(PrivilegeConstants.VIEW_PROVIDERS); + } + if (providers.size() > 1) { + log.warn("Can't handle users with multiple provider accounts"); + } else if (providers.size() == 1) { + currentProvider = providers.iterator().next(); + } + } + return currentProvider; + } +} \ No newline at end of file diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java new file mode 100644 index 000000000..60afacb4b --- /dev/null +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java @@ -0,0 +1,202 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9; + +import org.apache.commons.beanutils.PropertyUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.GlobalProperty; +import org.openmrs.Location; +import org.openmrs.PersonName; +import org.openmrs.api.APIException; +import org.openmrs.api.context.Context; +import org.openmrs.layout.web.name.NameSupport; +import org.openmrs.layout.web.name.NameTemplate; +import org.openmrs.util.OpenmrsConstants; +import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.mock.web.MockServletContext; +import org.springframework.web.context.request.ServletWebRequest; +import org.springframework.web.context.request.WebRequest; + +import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +@SuppressWarnings("unchecked") +public class SessionController1_9Test extends BaseModuleWebContextSensitiveTest { + + private static final String SESSION_ID = "test-session-id"; + + private static final String UNKNOWN_LOCATION_UUID = "8d6c993e-c2cc-11de-8d13-0010c6dffd0f"; // Unknown Location + + private static final String XANADU_UUID = "9356400c-a5a2-4532-8f2b-2361b3446eb8"; // Xanadu + + private SessionController1_9 controller; + + private HttpServletRequest hsr; + + @Before + public void before() { + controller = Context.getRegisteredComponents(SessionController1_9.class).iterator().next(); // should only be 1 + MockHttpServletRequest mockHsr = new MockHttpServletRequest(); + mockHsr.setSession(new MockHttpSession(new MockServletContext(), SESSION_ID)); + hsr = mockHsr; + + Context.getAdministrationService().saveGlobalProperty( + new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, sp, fr")); + Context.getUserContext().setLocation(Context.getLocationService().getLocationByUuid(UNKNOWN_LOCATION_UUID)); + } + + /** + * @see SessionController1_9#delete(HttpServletRequest) + * @verifies log the client out + */ + @Test + public void delete_shouldLogTheClientOut() throws Exception { + Assert.assertTrue(Context.isAuthenticated()); + controller.delete(hsr); + Assert.assertFalse(Context.isAuthenticated()); + Assert.assertNull(hsr.getSession(false)); + } + + /** + * @see SessionController1_9#get() + * @verifies return the session id if the user is authenticated + */ + @Test + public void get_shouldReturnTheUserIfTheUserIsAuthenticated() throws Exception { + Assert.assertTrue(Context.isAuthenticated()); + Object ret = controller.get(); + Object userProp = PropertyUtils.getProperty(ret, "user"); + List> userRoles = (List>) PropertyUtils.getProperty(userProp, + "roles"); + Assert.assertEquals("System Developer", userRoles.get(0).get("name")); + Assert.assertEquals(true, PropertyUtils.getProperty(ret, "authenticated")); + Assert.assertEquals(Context.getAuthenticatedUser().getUuid(), PropertyUtils.getProperty(userProp, "uuid")); + Object personProp = PropertyUtils.getProperty(userProp, "person"); + Assert.assertEquals(Context.getAuthenticatedUser().getPerson().getUuid(), + PropertyUtils.getProperty(personProp, "uuid")); + } + + @Test + public void get_shouldReturnLocaleInfoIfTheUserIsAuthenticated() throws Exception { + Assert.assertTrue(Context.isAuthenticated()); + Object ret = controller.get(); + Assert.assertEquals(Context.getLocale(), PropertyUtils.getProperty(ret, "locale")); + Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(), + ((List) PropertyUtils.getProperty(ret, "allowedLocales")).toArray()); + } + + @Test + public void get_shouldReturnLocationIfTheUserIsAuthenticated() throws Exception { + Assert.assertTrue(Context.isAuthenticated()); + Object ret = controller.get(); + Object loc = PropertyUtils.getProperty(ret, "sessionLocation"); + Assert.assertTrue(loc.toString() + " should contain 'display=Unknown Location'", + loc.toString().contains("display=Unknown Location")); + } + + /** + * @see SessionController1_9#get() + * @verifies return the session with current provider if the user is authenticated + */ + @Test + public void get_shouldReturnCurrentProviderIfTheUserIsAuthenticated() throws Exception { + Assert.assertTrue(Context.isAuthenticated()); + Object ret = controller.get(); + Object currentProvider = PropertyUtils.getProperty(ret, "currentProvider"); + Assert.assertNotNull(currentProvider); + Assert.assertTrue(currentProvider.toString().contains("Super User")); + } + + @Test + public void post_shouldReturnTheCurrentSession() throws Exception{ + String content = "{}"; + Object ret = controller.post(hsr,new ObjectMapper().readValue(content, HashMap.class)); + Object currentProvider = PropertyUtils.getProperty(ret, "currentProvider"); + Assert.assertNotNull(currentProvider); + Assert.assertTrue(currentProvider.toString().contains("Super User")); + } + + /** + * @see SessionController1_9#post(HttpServletRequest, Map) + * @verifies return the session with user.person.display formatted by nametemplate + */ + @Test + public void post_shouldReturnSessionUserPersonDisplayFormattedByNameTemplate() throws Exception { + executeDataSet("customTestDataset1_9.xml"); + + Context.logout(); + Context.authenticate("mujuzi", "test"); + Assert.assertTrue(Context.isAuthenticated()); + + String content = "{}"; + Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + Object userProp = PropertyUtils.getProperty(ret, "user"); + Object personProp = PropertyUtils.getProperty(userProp, "person"); + Object displayProp = PropertyUtils.getProperty(personProp, "display"); + + PersonName personName = Context.getAuthenticatedUser().getPersonName(); + NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate(); + + Assert.assertEquals(displayProp, nameTemplate.format(personName)); + Assert.assertEquals(displayProp, "Moses Tusha Mujuzi"); + } + + @Test + public void post_shouldSetTheUserLocale() throws Exception { + Locale newLocale = new Locale("sp"); + String content = "{\"locale\":\"" + newLocale.toString() + "\"}"; + Assert.assertNotEquals(newLocale, Context.getLocale()); + Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + Assert.assertEquals(newLocale, Context.getLocale()); + Assert.assertEquals(Context.getLocale(), PropertyUtils.getProperty(ret, "locale")); + Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(), + ((List) PropertyUtils.getProperty(ret, "allowedLocales")).toArray()); + } + + @Test(expected = APIException.class) + public void post_shouldFailWhenSettingIllegalLocale() throws Exception { + String newLocale = "fOOb@r:"; + String content = "{\"locale\":\"" + newLocale + "\"}"; + controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + } + + @Test(expected = APIException.class) + public void post_shouldFailWhenSettingDisallowedLocale() throws Exception { + String newLocale = "km_KH"; + String content = "{\"locale\":\"" + newLocale + "\"}"; + controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + } + + @Test + public void post_shouldSetTheSessionLocation() throws Exception { + String content = "{\"sessionLocation\":\"" + XANADU_UUID + "\"}"; + Location loc = Context.getLocationService().getLocationByUuid(XANADU_UUID); + Assert.assertNotEquals(loc, Context.getUserContext().getLocation()); + Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + Assert.assertEquals(loc, Context.getUserContext().getLocation()); + Object responseLoc = PropertyUtils.getProperty(ret, "sessionLocation"); + Assert.assertTrue(responseLoc.toString() + " should contain 'display=Xanadu'", + responseLoc.toString().contains("display=Xanadu")); + } + + @Test(expected = APIException.class) + public void post_shouldFailWhenSettingNonexistantLocation() throws Exception { + String content = "{\"sessionLocation\":\"fake-nonexistant-uuid\"}"; + controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + } +} \ No newline at end of file diff --git a/omod-1.9/src/test/resources/customTestDataset1_9.xml b/omod-1.9/src/test/resources/customTestDataset1_9.xml index e64953915..fca7ec8ed 100644 --- a/omod-1.9/src/test/resources/customTestDataset1_9.xml +++ b/omod-1.9/src/test/resources/customTestDataset1_9.xml @@ -41,4 +41,8 @@ + + + + diff --git a/omod-2.7/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_7/PersonResource2_7.java b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_0/PersonResource2_0.java similarity index 69% rename from omod-2.7/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_7/PersonResource2_7.java rename to omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_0/PersonResource2_0.java index 31ef27a5e..1d0a3d617 100644 --- a/omod-2.7/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_7/PersonResource2_7.java +++ b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_0/PersonResource2_0.java @@ -7,44 +7,41 @@ * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS * graphic logo is a trademark of OpenMRS Inc. */ -package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_7; +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_0; + +import java.lang.reflect.Method; import org.openmrs.Person; import org.openmrs.PersonName; import org.openmrs.layout.name.NameSupport; import org.openmrs.layout.name.NameTemplate; import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; import org.openmrs.module.webservices.rest.web.annotation.Resource; import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11.PersonResource1_11; @Resource(name = RestConstants.VERSION_1 + "/person", order = 0, supportedClass = Person.class, supportedOpenmrsVersions = { - "2.7.* - 9.*" }) -public class PersonResource2_7 extends PersonResource1_11 { + "2.0.* - 2.1.*" }) +public class PersonResource2_0 extends PersonResource1_11 { - /** - * Gets the display string for a person name. - * - * @param person the person whose person name will be displayed. - * @return fullName (for concise display purposes) - */ - @PropertyGetter("display") + @Override public String getDisplayString(Person person) { - if (person.getPersonName() == null) { + if (person.getPersonName() == null) return ""; - } PersonName personName = person.getPersonName(); try { NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate(); + if (nameTemplate != null) { - return nameTemplate.format(personName); + // need to use reflection since the format method was not added until later versions of openmrs + Method format = NameTemplate.class.getDeclaredMethod("format", PersonName.class); + return (String) format.invoke(nameTemplate, personName); } - } catch (Exception e) { + } + catch (Exception e) { // fall through to just returning full name if no format method found or format fails } - // otherwise, just return full name - return person.getPersonName().getFullName(); + return personName.getFullName(); } } diff --git a/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/PersonResource2_2.java b/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/PersonResource2_2.java index e96e51483..cdcde441a 100644 --- a/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/PersonResource2_2.java +++ b/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/PersonResource2_2.java @@ -17,14 +17,14 @@ import org.openmrs.module.webservices.rest.web.representation.Representation; import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; -import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11.PersonResource1_11; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_0.PersonResource2_0; /** * {@link Resource} for Person, supporting standard CRUD operations */ @Resource(name = RestConstants.VERSION_1 + "/person", supportedClass = Person.class, supportedOpenmrsVersions = { "2.2.* - 9.*" }) -public class PersonResource2_2 extends PersonResource1_11 { +public class PersonResource2_2 extends PersonResource2_0 { /** * @see DelegatingCrudResource#getRepresentationDescription(Representation) diff --git a/omod-2.7/pom.xml b/omod-2.7/pom.xml deleted file mode 100644 index b398608d9..000000000 --- a/omod-2.7/pom.xml +++ /dev/null @@ -1,214 +0,0 @@ - - - 4.0.0 - - webservices.rest - org.openmrs.module - 2.43.0-SNAPSHOT - - webservices.rest-omod-2.7 - jar - Rest Web Services 2.7 OMOD - OpenMRS module project for Rest Web Services - - - 2.7.0-SNAPSHOT - 1.8 - 1.8 - - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-common - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-common - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.8 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.8 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.9 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.9 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.10 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.10 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.11 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.11 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.0 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.0 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.2 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.2 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.3 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.3 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.4 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.4 - ${project.parent.version} - tests - test - - - - org.openmrs.api - openmrs-api - ${openmrs.version.2.7.0} - - - - org.openmrs.api - openmrs-api - test-jar - test - ${openmrs.version.2.7.0} - - - - org.openmrs.web - openmrs-web - ${openmrs.version.2.7.0} - - - - org.openmrs.web - openmrs-web - test-jar - test - ${openmrs.version.2.7.0} - - - - org.openmrs.test - openmrs-test - pom - test - ${openmrs.version.2.7.0} - - - - javax.servlet - javax.servlet-api - ${javaxVersion} - test - - - - org.apache.tomcat - jasper - ${apacheTomcatVersion} - provided - - - - - - - - org.jacoco - jacoco-maven-plugin - - - com.mycila - license-maven-plugin - -
${project.parent.basedir}/license-header.txt
-
-
-
-
-
\ No newline at end of file diff --git a/omod-2.7/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_7/SessionController2_7.java b/omod-2.7/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_7/SessionController2_7.java deleted file mode 100644 index f2b48dda3..000000000 --- a/omod-2.7/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_7/SessionController2_7.java +++ /dev/null @@ -1,152 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_7; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpSession; -import java.util.Collection; -import java.util.HashSet; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -import org.apache.commons.lang3.LocaleUtils; -import org.openmrs.Location; -import org.openmrs.Provider; -import org.openmrs.User; -import org.openmrs.api.APIException; -import org.openmrs.api.context.Context; -import org.openmrs.module.webservices.rest.SimpleObject; -import org.openmrs.module.webservices.rest.web.ConversionUtil; -import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation; -import org.openmrs.module.webservices.rest.web.representation.Representation; -import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController; -import org.openmrs.util.PrivilegeConstants; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; - -/** - * Controller that lets a client check the status of their session, and log out. (Authenticating is - * handled through a filter, and may happen through this or any other resource. - */ -@Controller("webservices.rest.sessionController2_7") -@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/session") -public class SessionController2_7 extends BaseRestController { - - private static final Logger log = LoggerFactory.getLogger(SessionController2_7.class); - - public static final String USER_CUSTOM_REP = "(uuid,display,username,systemId,userProperties,person:(uuid,display),privileges:(uuid,display,name),roles:(uuid,display,name),links)"; - - /** - * Tells the user whether they are authenticated and provides details on the logged-in user - */ - @RequestMapping(method = RequestMethod.GET) - @ResponseBody - public Object get() { - boolean authenticated = Context.isAuthenticated(); - SimpleObject session = new SimpleObject(); - session.add("authenticated", authenticated); - if (authenticated) { - session.add("user", ConversionUtil.convertToRepresentation(Context.getAuthenticatedUser(), - new CustomRepresentation(USER_CUSTOM_REP))); - session.add("locale", Context.getLocale()); - session.add("allowedLocales", Context.getAdministrationService().getAllowedLocales()); - session.add("sessionLocation", ConversionUtil.convertToRepresentation(Context.getUserContext().getLocation(), Representation.REF)); - session.add("currentProvider", ConversionUtil.convertToRepresentation(getCurrentProvider(), Representation.REF)); - } - return session; - } - - @RequestMapping(method = RequestMethod.POST) - @ResponseBody - @ResponseStatus(value = HttpStatus.OK) - public Object post(HttpServletRequest request, @RequestBody Map body) { - String localeStr = body.get("locale"); - if (localeStr != null) { - Locale locale = null; - try { - locale = LocaleUtils.toLocale(localeStr); - } - catch (IllegalArgumentException e) { - throw new APIException(" '" + localeStr + "' does not represent a valid locale."); - } - Set allowedLocales = new HashSet(Context.getAdministrationService().getAllowedLocales()); - if (allowedLocales.contains(locale)) { - Context.setLocale(locale); - } else { - throw new APIException(" '" + localeStr + "' is not in the list of allowed locales."); - } - } - String locationUuid = body.get("sessionLocation"); - if (locationUuid != null) { - Location location = Context.getLocationService().getLocationByUuid(locationUuid); - if (location == null) { - throw new APIException(" '" + locationUuid + "' is not the UUID of any location."); - } - Context.getUserContext().setLocation(location); - { // for compatability with AppUi session location - request.getSession().setAttribute("emrContext.sessionLocationId", location.getId()); - } - } - return get(); - } - - /** - * Logs the client out - * - * Should log the client out - */ - @RequestMapping(method = RequestMethod.DELETE) - @ResponseBody - @ResponseStatus(value = HttpStatus.NO_CONTENT) - public void delete(HttpServletRequest request) { - Context.logout(); - HttpSession session = request.getSession(false); - if (session != null && request.isRequestedSessionIdValid()) { - session.invalidate(); - } - } - - /** - * Get current provider - * - * @return Provider if the user is authenticated - */ - private Provider getCurrentProvider() { - Provider currentProvider = null; - User currentUser = Context.getAuthenticatedUser(); - if (currentUser != null) { - Collection providers = new HashSet(); - try { - Context.addProxyPrivilege(PrivilegeConstants.GET_PROVIDERS); - if (currentUser.getPerson() != null) { - providers = Context.getProviderService().getProvidersByPerson(currentUser.getPerson(), false); - } - } - finally { - Context.removeProxyPrivilege(PrivilegeConstants.GET_PROVIDERS); - } - if (providers.size() > 1) { - log.warn("Can't handle users with multiple provider accounts"); - } else if (providers.size() == 1) { - currentProvider = providers.iterator().next(); - } - } - return currentProvider; - } -} diff --git a/omod-2.7/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_7/SessionController2_7Test.java b/omod-2.7/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_7/SessionController2_7Test.java deleted file mode 100644 index 903ddd8f6..000000000 --- a/omod-2.7/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_7/SessionController2_7Test.java +++ /dev/null @@ -1,212 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_7; - -import javax.servlet.http.HttpServletRequest; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -import org.apache.commons.beanutils.PropertyUtils; -import org.codehaus.jackson.map.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.openmrs.GlobalProperty; -import org.openmrs.Location; -import org.openmrs.PersonName; -import org.openmrs.api.APIException; -import org.openmrs.api.context.Context; -import org.openmrs.api.context.UsernamePasswordCredentials; -import org.openmrs.layout.name.NameSupport; -import org.openmrs.layout.name.NameTemplate; -import org.openmrs.util.OpenmrsConstants; -import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpSession; -import org.springframework.mock.web.MockServletContext; - -/** - * Integration tests for the SessionController2_7 class - */ -@SuppressWarnings("unchecked") -public class SessionController2_7Test extends BaseModuleWebContextSensitiveTest { - - private static final String SESSION_ID = "test-session-id"; - - private static final String UNKNOWN_LOCATION_UUID = "8d6c993e-c2cc-11de-8d13-0010c6dffd0f"; // Unknown Location - - private static final String XANADU_UUID = "9356400c-a5a2-4532-8f2b-2361b3446eb8"; // Xanadu - - private SessionController2_7 controller; - - private HttpServletRequest hsr; - - private NameSupport nameSupport; - - @Before - public void before() { - executeDataSet("sessionControllerTestDataset.xml"); - - controller = Context.getRegisteredComponents(SessionController2_7.class).iterator().next(); - MockHttpServletRequest mockHsr = new MockHttpServletRequest(); - mockHsr.setSession(new MockHttpSession(new MockServletContext(), SESSION_ID)); - hsr = mockHsr; - - Context.getAdministrationService().saveGlobalProperty( - new GlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LOCALE_ALLOWED_LIST, "en_GB, sp, fr")); - Context.getUserContext().setLocation(Context.getLocationService().getLocationByUuid(UNKNOWN_LOCATION_UUID)); - - nameSupport = NameSupport.getInstance(); - nameSupport.setSpecialTokens(Arrays.asList("prefix", "givenName", "middleName", "familyNamePrefix", "familyName", - "familyName2", "familyNameSuffix", "degree")); - } - - /** - * @see SessionController2_7#delete(HttpServletRequest) - * @verifies log the client out - */ - @Test - public void delete_shouldLogTheClientOut() throws Exception { - Assert.assertTrue(Context.isAuthenticated()); - controller.delete(hsr); - Assert.assertFalse(Context.isAuthenticated()); - Assert.assertNull(hsr.getSession(false)); - } - - /** - * @see SessionController2_7#get() - * @verifies return the session id if the user is authenticated - */ - @Test - public void get_shouldReturnTheUserIfTheUserIsAuthenticated() throws Exception { - Assert.assertTrue(Context.isAuthenticated()); - Object ret = controller.get(); - Object userProp = PropertyUtils.getProperty(ret, "user"); - List> userRoles = (List>) PropertyUtils.getProperty(userProp, - "roles"); - Assert.assertEquals("System Developer", userRoles.get(0).get("name")); - Assert.assertEquals(true, PropertyUtils.getProperty(ret, "authenticated")); - Assert.assertEquals(Context.getAuthenticatedUser().getUuid(), PropertyUtils.getProperty(userProp, "uuid")); - Object personProp = PropertyUtils.getProperty(userProp, "person"); - Assert.assertEquals(Context.getAuthenticatedUser().getPerson().getUuid(), - PropertyUtils.getProperty(personProp, "uuid")); - } - - @Test - public void get_shouldReturnLocaleInfoIfTheUserIsAuthenticated() throws Exception { - Assert.assertTrue(Context.isAuthenticated()); - Object ret = controller.get(); - Assert.assertEquals(Context.getLocale(), PropertyUtils.getProperty(ret, "locale")); - Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(), - ((List) PropertyUtils.getProperty(ret, "allowedLocales")).toArray()); - } - - @Test - public void get_shouldReturnLocationIfTheUserIsAuthenticated() throws Exception { - Assert.assertTrue(Context.isAuthenticated()); - Object ret = controller.get(); - Object loc = PropertyUtils.getProperty(ret, "sessionLocation"); - Assert.assertNotNull(loc); - Assert.assertTrue(loc.toString().contains("display=Unknown Location")); - } - - /** - * @see SessionController2_7#get() - * @verifies return the session with current provider if the user is authenticated - */ - @Test - public void get_shouldReturnCurrentProviderIfTheUserIsAuthenticated() throws Exception { - Assert.assertTrue(Context.isAuthenticated()); - Object ret = controller.get(); - Object currentProvider = PropertyUtils.getProperty(ret, "currentProvider"); - Assert.assertNotNull(currentProvider); - Assert.assertTrue(currentProvider.toString().contains("Super User")); - } - - @Test - public void post_shouldReturnTheCurrentSession() throws Exception{ - String content = "{}"; - Object ret = controller.post(hsr,new ObjectMapper().readValue(content, HashMap.class)); - Object currentProvider = PropertyUtils.getProperty(ret, "currentProvider"); - Assert.assertNotNull(currentProvider); - Assert.assertTrue(currentProvider.toString().contains("Super User")); - } - - /** - * @see SessionController2_7#post(HttpServletRequest, Map) - * @verifies return the session with user.person.display formatted by nametemplate - */ - @Test - public void post_shouldReturnSessionUserPersonDisplayFormattedByNameTemplate() throws Exception { - Context.logout(); - Context.authenticate(new UsernamePasswordCredentials("mujuzi", "test")); - Assert.assertTrue(Context.isAuthenticated()); - - String content = "{}"; - Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); - Object userProp = PropertyUtils.getProperty(ret, "user"); - Object personProp = PropertyUtils.getProperty(userProp, "person"); - Object displayProp = PropertyUtils.getProperty(personProp, "display"); - - PersonName personName = Context.getAuthenticatedUser().getPersonName(); - NameTemplate nameTemplate = nameSupport.getDefaultLayoutTemplate(); - - Assert.assertEquals(displayProp, nameTemplate.format(personName)); - Assert.assertEquals(displayProp, "Mr. Moses Mujuzi"); - } - - @Test - public void post_shouldSetTheUserLocale() throws Exception { - Locale newLocale = new Locale("sp"); - String content = "{\"locale\":\"" + newLocale.toString() + "\"}"; - Assert.assertNotEquals(newLocale, Context.getLocale()); - Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); - Assert.assertEquals(newLocale, Context.getLocale()); - Assert.assertEquals(Context.getLocale(), PropertyUtils.getProperty(ret, "locale")); - Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(), - ((List) PropertyUtils.getProperty(ret, "allowedLocales")).toArray()); - } - - @Test(expected = APIException.class) - public void post_shouldFailWhenSettingIllegalLocale() throws Exception { - String newLocale = "fOOb@r:"; - String content = "{\"locale\":\"" + newLocale + "\"}"; - controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); - } - - @Test(expected = APIException.class) - public void post_shouldFailWhenSettingDisallowedLocale() throws Exception { - String newLocale = "km_KH"; - String content = "{\"locale\":\"" + newLocale + "\"}"; - controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); - } - - @Test - public void post_shouldSetTheSessionLocation() throws Exception { - String content = "{\"sessionLocation\":\"" + XANADU_UUID + "\"}"; - Location loc = Context.getLocationService().getLocationByUuid(XANADU_UUID); - Assert.assertNotEquals(loc, Context.getUserContext().getLocation()); - Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); - Assert.assertEquals(loc, Context.getUserContext().getLocation()); - Object responseLoc = PropertyUtils.getProperty(ret, "sessionLocation"); - Assert.assertNotNull(responseLoc); - Assert.assertTrue(responseLoc.toString().contains("display=Xanadu")); - } - - @Test(expected = APIException.class) - public void post_shouldFailWhenSettingNonexistantLocation() throws Exception { - String content = "{\"sessionLocation\":\"fake-nonexistant-uuid\"}"; - controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); - } -} \ No newline at end of file diff --git a/omod-2.7/src/test/resources/sessionControllerTestDataset.xml b/omod-2.7/src/test/resources/sessionControllerTestDataset.xml deleted file mode 100644 index c17d44b57..000000000 --- a/omod-2.7/src/test/resources/sessionControllerTestDataset.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/omod/pom.xml b/omod/pom.xml index 2c4000ea5..8b47b7d58 100644 --- a/omod/pom.xml +++ b/omod/pom.xml @@ -143,18 +143,6 @@ tests test - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.7 - ${project.parent.version} - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.7 - ${project.parent.version} - tests - test - diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index 7eb4678f4..0b2d6222f 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -50,10 +50,6 @@ /lib/webservices.rest-omod-2.4.* 2.4.* - 9.* - - /lib/webservices.rest-omod-2.7.* - 2.7.* - 9.* - diff --git a/pom.xml b/pom.xml index ccdcb7a5b..0400fda16 100644 --- a/pom.xml +++ b/pom.xml @@ -428,7 +428,6 @@ omod-2.2 omod-2.3 omod-2.4 - omod-2.7 omod integration-tests @@ -502,7 +501,6 @@ omod-2.2 omod-2.3 omod-2.4 - omod-2.7 omod integration-tests