Search in sources :

Example 1 with EmployeeList

use of org.springframework.integration.samples.rest.domain.EmployeeList in project spring-integration-samples by spring-projects.

the class EmployeeSearchService method getEmployee.

/**
 * The API <code>getEmployee()</code> looks up the mapped in coming message header's id param
 * and fills the return object with the appropriate employee details. The return
 * object is wrapped in Spring Integration Message with response headers filled in.
 * This example shows the usage of URL path variables and how the service act on
 * those variables.
 * @param inMessage
 * @return an instance of <code>{@link Message}</code> that wraps <code>{@link EmployeeList}</code>
 */
@Secured("ROLE_REST_HTTP_USER")
public Message<EmployeeList> getEmployee(Message<?> inMessage) {
    EmployeeList employeeList = new EmployeeList();
    Map<String, Object> responseHeaderMap = new HashMap<String, Object>();
    try {
        MessageHeaders headers = inMessage.getHeaders();
        String id = (String) headers.get("employeeId");
        boolean isFound;
        if (id.equals("1")) {
            employeeList.getEmployee().add(new Employee(1, "John", "Doe"));
            isFound = true;
        } else if (id.equals("2")) {
            employeeList.getEmployee().add(new Employee(2, "Jane", "Doe"));
            isFound = true;
        } else if (id.equals("0")) {
            employeeList.getEmployee().add(new Employee(1, "John", "Doe"));
            employeeList.getEmployee().add(new Employee(2, "Jane", "Doe"));
            isFound = true;
        } else {
            isFound = false;
        }
        if (isFound) {
            setReturnStatusAndMessage("0", "Success", employeeList, responseHeaderMap);
        } else {
            setReturnStatusAndMessage("2", "Employee Not Found", employeeList, responseHeaderMap);
        }
    } catch (Exception e) {
        setReturnStatusAndMessage("1", "System Error", employeeList, responseHeaderMap);
        logger.error("System error occured :" + e);
    }
    Message<EmployeeList> message = new GenericMessage<EmployeeList>(employeeList, responseHeaderMap);
    return message;
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) Employee(org.springframework.integration.samples.rest.domain.Employee) HashMap(java.util.HashMap) MessageHeaders(org.springframework.messaging.MessageHeaders) EmployeeList(org.springframework.integration.samples.rest.domain.EmployeeList) Secured(org.springframework.security.access.annotation.Secured)

Example 2 with EmployeeList

use of org.springframework.integration.samples.rest.domain.EmployeeList in project spring-integration-samples by spring-projects.

the class RestHttpClientTest method testGetEmployeeAsXml.

/**
 * @throws Exception
 */
@Test
public void testGetEmployeeAsXml() {
    Map<String, Object> employeeSearchMap = getEmployeeSearchMap("0");
    final String fullUrl = "http://localhost:8080/rest-http/services/employee/{id}/search";
    EmployeeList employeeList = restTemplate.execute(fullUrl, HttpMethod.GET, request -> {
        HttpHeaders headers = getHttpHeadersWithUserCredentials(request);
        headers.add("Accept", "application/xml");
    }, responseExtractor, employeeSearchMap);
    logger.info("The employee list size :" + employeeList.getEmployee().size());
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    marshaller.marshal(employeeList, sr);
    logger.info(sr.getWriter().toString());
    assertTrue(employeeList.getEmployee().size() > 0);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) EmployeeList(org.springframework.integration.samples.rest.domain.EmployeeList) Test(org.junit.Test)

Aggregations

EmployeeList (org.springframework.integration.samples.rest.domain.EmployeeList)2 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Test (org.junit.Test)1 HttpHeaders (org.springframework.http.HttpHeaders)1 Employee (org.springframework.integration.samples.rest.domain.Employee)1 MessageHeaders (org.springframework.messaging.MessageHeaders)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1 Secured (org.springframework.security.access.annotation.Secured)1