@SuppressWarnings("unchecked") private PR getParentResource() { org.openmrs.module.webservices.rest.web.annotation.SubResource sub = getClass().getAnnotation( org.openmrs.module.webservices.rest.web.annotation.SubResource.class); org.openmrs.module.webservices.rest.web.annotation.Resource resource = sub.parent().getAnnotation( org.openmrs.module.webservices.rest.web.annotation.Resource.class); return (PR) Context.getService(RestService.class).getResourceByName(resource.name()); }
/** * @param uuid * @param request * @return * @throws ResponseException */ @RequestMapping(value = "/{resource}/{uuid}", method = RequestMethod.GET) @ResponseBody public Object retrieve(@PathVariable("resource") String resource, @PathVariable("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) throws ResponseException { baseUriSetup.setup(request); RequestContext context = RestUtil.getRequestContext(request, response); Retrievable res = (Retrievable) restService.getResourceByName(buildResourceName(resource)); return res.retrieve(uuid, context); }
/** * @param parentUuid * @param reason * @param request * @throws Exception */ @RequestMapping(value = "/{resource}/{parentUuid}/{subResource}", method = RequestMethod.DELETE, params = "!purge") @ResponseBody public Object delete(@PathVariable("resource") String resource, @PathVariable("parentUuid") String parentUuid, @PathVariable("subResource") String subResource, @RequestParam(value = "reason", defaultValue = "web service call") String reason, HttpServletRequest request, HttpServletResponse response) throws ResponseException { baseUriSetup.setup(request); RequestContext context = RestUtil.getRequestContext(request, response); SubResource res = (SubResource) restService.getResourceByName(buildResourceName(resource) + "/" + subResource); res.delete(parentUuid, null, reason, context); return RestUtil.noContent(response); }
/** * @param parentUuid * @param uuid * @param request * @param response * @throws Exception */ @RequestMapping(value = "/{resource}/{parentUuid}/{subResource}/{uuid}", method = RequestMethod.DELETE, params = "purge") @ResponseBody public Object purge(@PathVariable("resource") String resource, @PathVariable("parentUuid") String parentUuid, @PathVariable("subResource") String subResource, @PathVariable("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) throws ResponseException { baseUriSetup.setup(request); RequestContext context = RestUtil.getRequestContext(request, response); SubResource res = (SubResource) restService.getResourceByName(buildResourceName(resource) + "/" + subResource); res.purge(parentUuid, uuid, context); return RestUtil.noContent(response); }
@RequestMapping(value = "/{resource}", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data") @ResponseBody public Object upload(@PathVariable("resource") String resource, @RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException, ResponseException { baseUriSetup.setup(request); RequestContext context = RestUtil.getRequestContext(request, response); Resource res = restService.getResourceByName(buildResourceName(resource)); if (res instanceof Uploadable) { Object updated = ((Uploadable) res).upload(file, context); return RestUtil.created(response, updated); } else { throw new ResourceDoesNotSupportOperationException(res.getClass().getSimpleName() + "is not uploadable"); } }
/** * @param uuid * @param request * @param response * @throws Exception */ @RequestMapping(value = "/{resource}/{uuid}", method = RequestMethod.DELETE, params = "purge=true") @ResponseBody public Object purge(@PathVariable("resource") String resource, @PathVariable("uuid") String uuid, HttpServletRequest request, HttpServletResponse response) throws ResponseException { baseUriSetup.setup(request); RequestContext context = RestUtil.getRequestContext(request, response); Purgeable res = (Purgeable) restService.getResourceByName(buildResourceName(resource)); res.purge(uuid, context); return RestUtil.noContent(response); }
private Patient getPatient(String patientUniqueId) { Patient patient = ((PatientResource1_8) Context.getService(RestService.class).getResourceByName( RestConstants.VERSION_1 + "/patient")).getByUniqueId(patientUniqueId); if (patient == null) throw new ObjectNotFoundException(); return patient; }
/** * @see org.openmrs.module.webservices.rest.web.resource.api.Converter#asRepresentation(T, * org.openmrs.module.webservices.rest.web.representation.Representation) */ @Override public SimpleObject asRepresentation(User instance, Representation rep) throws ConversionException { UserAndPassword1_8 userPass = new UserAndPassword1_8(instance); UserResource1_8 userResource = (UserResource1_8) Context.getService(RestService.class).getResourceByName( RestConstants.VERSION_1 + "/user"); return userResource.asRepresentation(userPass, rep); }
/** * @see org.openmrs.module.webservices.rest.web.resource.api.Converter#getProperty(T, * java.lang.String) */ @Override public Object getProperty(User instance, String propertyName) throws ConversionException { UserAndPassword1_8 userPass = new UserAndPassword1_8(instance); UserResource1_8 userResource = (UserResource1_8) Context.getService(RestService.class).getResourceByName( RestConstants.VERSION_1 + "/user"); return userResource.getProperty(userPass, propertyName); }
private Location getLocation(String locationUniqueId) { Location location = ((LocationResource1_8) Context.getService(RestService.class).getResourceByName( RestConstants.VERSION_1 + "/location")).getByUniqueId(locationUniqueId); if (location == null) throw new ObjectNotFoundException(); return location; } }
/** * @see org.openmrs.module.webservices.rest.web.resource.api.Converter#setProperty(java.lang.Object, * java.lang.String, java.lang.Object) */ @Override public void setProperty(Object instance, String propertyName, Object value) throws ConversionException { UserAndPassword1_8 userPass = new UserAndPassword1_8((User) instance); UserResource1_8 userResource = (UserResource1_8) Context.getService(RestService.class).getResourceByName( RestConstants.VERSION_1 + "/user"); userResource.setProperty(userPass, propertyName, value); } }
/** * @verifies return resource for given name * @see RestServiceImpl#getResourceByName(String) */ @Test public void getResourceByName_shouldReturnResourceForGivenName() throws Exception { List<Class<? extends Resource>> resources = new ArrayList<Class<? extends Resource>>(); resources.add(AnimalResource_1_9.class); resources.add(AnimalResource_1_11.class); when(openmrsClassScanner.getClasses(Resource.class, true)).thenReturn(resources); setCurrentOpenmrsVersion("1.9.10"); assertThat(restService.getResourceByName("v1/animal"), instanceOf(AnimalResource_1_9.class)); }
/** * @verifies return resource for given name and ignore unannotated resources * @see RestServiceImpl#getResourceByName(String) */ @Test public void getResourceByName_shouldReturnResourceForGivenNameAndIgnoreUnannotatedResources() throws Exception { List<Class<? extends Resource>> resources = new ArrayList<Class<? extends Resource>>(); resources.add(AnimalResource_1_9.class); resources.add(AnimalResource_1_11.class); resources.add(UnannotatedAnimalResource.class); when(openmrsClassScanner.getClasses(Resource.class, true)).thenReturn(resources); setCurrentOpenmrsVersion("1.9.10"); assertThat(restService.getResourceByName("v1/animal"), instanceOf(AnimalResource_1_9.class)); }
/** * @verifies return subresource for given name * @see RestServiceImpl#getResourceByName(String) */ @Test public void getResourceByName_shouldReturnSubresourceForGivenName() throws Exception { List<Class<? extends Resource>> resources = new ArrayList<Class<? extends Resource>>(); resources.add(AnimalResource_1_9.class); resources.add(AnimalResource_1_11.class); resources.add(AnimalClassResource_1_9.class); when(openmrsClassScanner.getClasses(Resource.class, true)).thenReturn(resources); setCurrentOpenmrsVersion("1.9.10"); assertThat(restService.getResourceByName("v1/animal/class"), instanceOf(AnimalClassResource_1_9.class)); }
/** * @verifies return resource with lower order value if two resources with the same name are * found for given name * @see RestServiceImpl#getResourceByName(String) */ @Test public void getResourceByName_shouldReturnResourceWithLowerOrderValueIfTwoResourcesWithTheSameNameAreFoundForGivenName() throws Exception { List<Class<? extends Resource>> resources = new ArrayList<Class<? extends Resource>>(); resources.add(AnimalResource_1_9.class); resources.add(DuplicateNameAnimalResource_1_9.class); when(openmrsClassScanner.getClasses(Resource.class, true)).thenReturn(resources); setCurrentOpenmrsVersion("1.9.10"); assertThat(restService.getResourceByName("v1/animal"), instanceOf(AnimalResource_1_9.class)); }
/** * @verifies fail if failed to get resource classes * @see RestServiceImpl#getResourceByName(String) */ @Test public void getResourceByName_shouldFailIfFailedToGetResourceClasses() throws Exception { IOException ioException = new IOException("some"); when(openmrsClassScanner.getClasses(Resource.class, true)).thenThrow(ioException); expectedException.expect(APIException.class); expectedException.expectMessage("Cannot access REST resources"); expectedException.expectCause(is(ioException)); restService.getResourceByName("v1/animal"); }
/** * @verifies fail if two resources with same name and order are found for given name * @see RestServiceImpl#getResourceByName(String) */ @Test public void getResourceByName_shouldFailIfTwoResourcesWithSameNameAndOrderAreFoundForGivenName() throws Exception { List<Class<? extends Resource>> resources = new ArrayList<Class<? extends Resource>>(); resources.add(AnimalResource_1_9.class); resources.add(DuplicateNameAndOrderAnimalResource_1_9.class); when(openmrsClassScanner.getClasses(Resource.class, true)).thenReturn(resources); setCurrentOpenmrsVersion("1.9.10"); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Two resources with the same name (v1/animal) must not have the same order"); restService.getResourceByName("v1/animal"); }
/** * @verifies fail if failed to get resource classes * @see RestServiceImpl#getResourceBySupportedClass(Class) */ @Test public void getResourceBySupportedClass_shouldFailIfFailedToGetResourceClasses() throws Exception { IOException ioException = new IOException("some"); when(openmrsClassScanner.getClasses(Resource.class, true)).thenThrow(ioException); expectedException.expect(APIException.class); expectedException.expectMessage("Cannot access REST resources"); expectedException.expectCause(is(ioException)); restService.getResourceByName("v1/animal"); }
/** * @verifies fail if resource for given name does not support the current openmrs version * @see RestServiceImpl#getResourceByName(String) */ @Test public void getResourceByName_shouldFailIfResourceForGivenNameDoesNotSupportTheCurrentOpenmrsVersion() throws Exception { List<Class<? extends Resource>> resources = new ArrayList<Class<? extends Resource>>(); resources.add(AnimalResource_1_9.class); resources.add(AnimalResource_1_11.class); when(openmrsClassScanner.getClasses(Resource.class, true)).thenReturn(resources); setCurrentOpenmrsVersion("1.12.0"); expectedException.expect(UnknownResourceException.class); expectedException.expectMessage("Unknown resource: v1/animal"); restService.getResourceByName("v1/animal"); }
/** * @verifies fail if resource for given name cannot be found * @see RestServiceImpl#getResourceByName(String) */ @Test public void getResourceByName_shouldFailIfResourceForGivenNameCannotBeFound() throws Exception { expectedException.expect(UnknownResourceException.class); expectedException.expectMessage("Unknown resource: UNKNOWNRESOURCENAME"); restService.getResourceByName("UNKNOWNRESOURCENAME"); }