Search in sources :

Example 1 with KeyValuePair

use of org.olat.restapi.support.vo.KeyValuePair in project OpenOLAT by OpenOLAT.

the class CourseDBTest method getUsedCategories.

@Test
public void getUsedCategories() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(auth.getName(), JunitTestHelper.PWD));
    String category = createRndCategory();
    KeyValuePair keyValuePair = new KeyValuePair();
    keyValuePair.setKey("catKey");
    keyValuePair.setValue("category value");
    UriBuilder uri = getUriBuilder(course.getResourceableId(), category).path("values");
    HttpPut put = conn.createPut(uri.build(), MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(put, keyValuePair);
    HttpResponse response = conn.execute(put);
    assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    conn.shutdown();
    List<String> categories = courseDbManager.getUsedCategories(course);
    Assert.assertNotNull(categories);
    Assert.assertTrue(categories.contains(category));
}
Also used : KeyValuePair(org.olat.restapi.support.vo.KeyValuePair) HttpResponse(org.apache.http.HttpResponse) UriBuilder(javax.ws.rs.core.UriBuilder) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 2 with KeyValuePair

use of org.olat.restapi.support.vo.KeyValuePair in project OpenOLAT by OpenOLAT.

the class CourseDbWebService method getValue.

/**
 * Retrieve a value of an authenticated user.
 * @response.representation.200.qname {http://www.example.com}keyValuePair
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The value in the course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_KEYVALUEVO}
 * @response.representation.404.doc The entry cannot be found
 * @param courseId The course resourceable's id
 * @param category The name of the database
 * @parma name The name of the key value pair
 * @param request The HTTP request
 * @return
 */
@GET
@Path("values/{name}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getValue(@PathParam("courseId") Long courseId, @PathParam("category") String category, @PathParam("name") String name, @Context HttpServletRequest request) {
    ICourse course = loadCourse(courseId);
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    CourseDBEntry entry = CoreSpringFactory.getImpl(CourseDBManager.class).getValue(course, ureq.getIdentity(), category, name);
    if (entry == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Object value = entry.getValue();
    KeyValuePair pair = new KeyValuePair(name, value == null ? "" : value.toString());
    return Response.ok(pair).build();
}
Also used : CourseDBManager(org.olat.course.db.CourseDBManager) KeyValuePair(org.olat.restapi.support.vo.KeyValuePair) ICourse(org.olat.course.ICourse) UserRequest(org.olat.core.gui.UserRequest) CourseDBEntry(org.olat.course.db.CourseDBEntry) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with KeyValuePair

use of org.olat.restapi.support.vo.KeyValuePair in project openolat by klemens.

the class CourseDBTest method createEntry_putJsonEntity.

@Test
public void createEntry_putJsonEntity() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(auth.getName(), JunitTestHelper.PWD));
    String category = createRndCategory();
    KeyValuePair keyValuePair = new KeyValuePair();
    keyValuePair.setKey("firstKey");
    keyValuePair.setValue("first value");
    UriBuilder uri = getUriBuilder(course.getResourceableId(), category).path("values");
    HttpPut put = conn.createPut(uri.build(), MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(put, keyValuePair);
    HttpResponse response = conn.execute(put);
    assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    conn.shutdown();
}
Also used : KeyValuePair(org.olat.restapi.support.vo.KeyValuePair) HttpResponse(org.apache.http.HttpResponse) UriBuilder(javax.ws.rs.core.UriBuilder) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 4 with KeyValuePair

use of org.olat.restapi.support.vo.KeyValuePair in project openolat by klemens.

the class CourseDBTest method createEntry_get.

@Test
public void createEntry_get() throws IOException, URISyntaxException {
    String category = createRndCategory();
    String key = "getit";
    String value = "get a value";
    CourseDBEntry entry = courseDbManager.setValue(course, auth, category, key, value);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(entry);
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login(auth.getName(), JunitTestHelper.PWD));
    UriBuilder uri = getUriBuilder(course.getResourceableId(), category).path("values").path(key);
    HttpGet get = conn.createGet(uri.build(), MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(get);
    assertEquals(200, response.getStatusLine().getStatusCode());
    KeyValuePair savedEntry = conn.parse(response, KeyValuePair.class);
    conn.shutdown();
    Assert.assertNotNull(savedEntry);
    Assert.assertEquals(key, savedEntry.getKey());
    Assert.assertEquals(value, savedEntry.getValue());
}
Also used : KeyValuePair(org.olat.restapi.support.vo.KeyValuePair) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) UriBuilder(javax.ws.rs.core.UriBuilder) CourseDBEntry(org.olat.course.db.CourseDBEntry) Test(org.junit.Test)

Example 5 with KeyValuePair

use of org.olat.restapi.support.vo.KeyValuePair in project openolat by klemens.

the class CourseDbWebService method getValue.

/**
 * Retrieve a value of an authenticated user.
 * @response.representation.200.qname {http://www.example.com}keyValuePair
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The value in the course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_KEYVALUEVO}
 * @response.representation.404.doc The entry cannot be found
 * @param courseId The course resourceable's id
 * @param category The name of the database
 * @parma name The name of the key value pair
 * @param request The HTTP request
 * @return
 */
@GET
@Path("values/{name}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getValue(@PathParam("courseId") Long courseId, @PathParam("category") String category, @PathParam("name") String name, @Context HttpServletRequest request) {
    ICourse course = loadCourse(courseId);
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    CourseDBEntry entry = CoreSpringFactory.getImpl(CourseDBManager.class).getValue(course, ureq.getIdentity(), category, name);
    if (entry == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Object value = entry.getValue();
    KeyValuePair pair = new KeyValuePair(name, value == null ? "" : value.toString());
    return Response.ok(pair).build();
}
Also used : CourseDBManager(org.olat.course.db.CourseDBManager) KeyValuePair(org.olat.restapi.support.vo.KeyValuePair) ICourse(org.olat.course.ICourse) UserRequest(org.olat.core.gui.UserRequest) CourseDBEntry(org.olat.course.db.CourseDBEntry) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

KeyValuePair (org.olat.restapi.support.vo.KeyValuePair)10 UriBuilder (javax.ws.rs.core.UriBuilder)6 HttpResponse (org.apache.http.HttpResponse)6 Test (org.junit.Test)6 CourseDBEntry (org.olat.course.db.CourseDBEntry)6 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 HttpPut (org.apache.http.client.methods.HttpPut)4 UserRequest (org.olat.core.gui.UserRequest)4 ICourse (org.olat.course.ICourse)4 CourseDBManager (org.olat.course.db.CourseDBManager)4 HttpGet (org.apache.http.client.methods.HttpGet)2