Search in sources :

Example 1 with UserDto

use of com.networknt.portal.usermanagement.model.common.domain.UserDto in project light-portal by networknt.

the class UserIdPutHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    String id = exchange.getQueryParameters().get("id").getFirst();
    ObjectMapper mapper = new ObjectMapper();
    Map s = (Map) exchange.getAttachment(BodyHandler.REQUEST_BODY);
    String json = mapper.writeValueAsString(s);
    UserDto user = mapper.readValue(json, UserDto.class);
    User userResult = null;
    try {
        if (user.isEmailChange()) {
            userResult = service.changeEmail(id, user.getContactData().getEmail());
        } else if (user.isPasswordReset()) {
            userResult = service.changePassword(id, user.getPassword());
        } else if (user.isScreenNameChange()) {
            userResult = service.changeScreenName(id, user.getScreenName());
        } else {
            userResult = service.fromUserDto(user);
            userResult.setId(id);
            userResult = service.update(userResult);
        }
    } catch (NoSuchUserException e) {
    // TODO handler excption, add log info?
    }
    String result;
    if (userResult == null) {
        result = "no user changed;";
    } else {
        result = Config.getInstance().getMapper().writeValueAsString(service.toUserDto(userResult));
    }
    exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
    exchange.getResponseSender().send(result);
// exchange.endExchange();
}
Also used : User(com.networknt.portal.usermanagement.model.common.model.user.User) UserDto(com.networknt.portal.usermanagement.model.common.domain.UserDto) NoSuchUserException(com.networknt.portal.usermanagement.model.common.exception.NoSuchUserException) HttpString(io.undertow.util.HttpString) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpString(io.undertow.util.HttpString)

Example 2 with UserDto

use of com.networknt.portal.usermanagement.model.common.domain.UserDto in project light-portal by networknt.

the class UserPostHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    // add a new object
    Map s = (Map) exchange.getAttachment(BodyHandler.REQUEST_BODY);
    String json = mapper.writeValueAsString(s);
    UserDto userDto = mapper.readValue(json, UserDto.class);
    String result = "Ok!";
    try {
        User user = service.fromUserDto(userDto);
        service.signup(user, userDto.getPassword(), false);
        // TODO remove the following implemetation after confirm email implemented
        Optional<ConfirmationToken> token = user.getConfirmationTokens().stream().findFirst();
        if (token.isPresent()) {
            result = "http://localhost:8080/v1/user/token/" + token.get().getId();
        }
    } catch (Exception e) {
        result = e.getMessage();
    // TODO handler excption, add log info?
    }
    exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
    exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(result));
// exchange.endExchange();
}
Also used : User(com.networknt.portal.usermanagement.model.common.model.user.User) ConfirmationToken(com.networknt.portal.usermanagement.model.common.model.user.ConfirmationToken) UserDto(com.networknt.portal.usermanagement.model.common.domain.UserDto) HttpString(io.undertow.util.HttpString) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) NoSuchUserException(com.networknt.portal.usermanagement.model.common.exception.NoSuchUserException) HttpString(io.undertow.util.HttpString)

Example 3 with UserDto

use of com.networknt.portal.usermanagement.model.common.domain.UserDto in project light-portal by networknt.

the class UserIdPutHandlerTest method testUserIdPutHandlerTestUserUpdate.

@Test
public void testUserIdPutHandlerTestUserUpdate() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    UserDto userDto = new UserDto("aaa.bbb@gmail.com", "testUser");
    userDto.setHost("google");
    userDto.setPassword("12345678");
    userDto.getContactData().setFirstName("test1");
    userDto.getContactData().setLastName("bbb1");
    userDto.getContactData().setGender(Gender.MALE);
    AddressData address = new AddressData();
    address.setCountry(Country.CA);
    address.setState(State.AK);
    address.setCity("BaBa");
    address.setAddressType(AddressType.SHIPPING);
    address.setAddressLine1("222 Bay Street");
    userDto.getContactData().addAddresses(address);
    String json = JSonMapper.toJson(userDto);
    System.out.println(json);
    try {
        ClientRequest request = new ClientRequest().setPath("/v1/user/1233333").setMethod(Methods.PUT);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, json));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    System.out.println("response:" + body);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UserDto(com.networknt.portal.usermanagement.model.common.domain.UserDto) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) SQLException(java.sql.SQLException) ApiException(com.networknt.exception.ApiException) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 4 with UserDto

use of com.networknt.portal.usermanagement.model.common.domain.UserDto in project light-portal by networknt.

the class UserPostHandlerTest method testUserPostHandlerTest.

@Test
public void testUserPostHandlerTest() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    UserDto userDto = new UserDto("aaa.bbb@gmail.com", "testUser");
    userDto.setHost("google");
    userDto.setPassword("12345678");
    userDto.getContactData().setFirstName("test2");
    userDto.getContactData().setLastName("bbb2");
    userDto.getContactData().setGender(Gender.MALE);
    AddressData address = new AddressData();
    address.setCountry(Country.CA);
    address.setState(State.AK);
    address.setCity("BaBa");
    address.setAddressType(AddressType.SHIPPING);
    address.setAddressLine1("222 Bay Street");
    userDto.getContactData().addAddresses(address);
    String json = JSonMapper.toJson(userDto);
    System.out.println(json);
    try {
        ClientRequest request = new ClientRequest().setPath("/v1/user").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, json));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) UserDto(com.networknt.portal.usermanagement.model.common.domain.UserDto) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ApiException(com.networknt.exception.ApiException) ClientConnection(io.undertow.client.ClientConnection) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 5 with UserDto

use of com.networknt.portal.usermanagement.model.common.domain.UserDto in project light-portal by networknt.

the class UserQueryWorkflow method create.

@EventHandlerMethod
public void create(DispatchedEvent<UserSignUpEvent> de) {
    UserSignUpEvent event = de.getEvent();
    String id = de.getEntityId();
    UserDto userDto = event.getUserDto();
    Int128 eventId = de.getEventId();
    logger.info("**************** account version={}, {}", id, eventId);
    String json = JSonMapper.toJson(userDto);
    try {
        User user = service.fromUserDto(userDto, id);
        service.signup(user, userDto.getPassword(), true);
        // TODO remove the following implemetation after confirm email implemented
        Optional<ConfirmationToken> token = user.getConfirmationTokens().stream().findFirst();
        if (token.isPresent()) {
            // TODO send email
            System.out.println("Link in the email:\n" + "http://localhost:8081/v1/user/token/" + user.getId() + "?token=" + token.get().getId());
        }
    } catch (Exception e) {
        System.out.println("error:" + e.getMessage());
        logger.error("user signup failed:", userDto + " error:" + e.getMessage());
    // TODO handler excption, add log info?
    }
}
Also used : User(com.networknt.portal.usermanagement.model.common.model.user.User) ConfirmationToken(com.networknt.portal.usermanagement.model.common.model.user.ConfirmationToken) UserDto(com.networknt.portal.usermanagement.model.common.domain.UserDto) UserSignUpEvent(com.networknt.portal.usermanagement.model.common.event.UserSignUpEvent) Int128(com.networknt.eventuate.common.Int128) InvalidEmailException(com.networknt.portal.usermanagement.model.common.exception.InvalidEmailException) NoSuchUserException(com.networknt.portal.usermanagement.model.common.exception.NoSuchUserException) EventHandlerMethod(com.networknt.eventuate.common.EventHandlerMethod)

Aggregations

UserDto (com.networknt.portal.usermanagement.model.common.domain.UserDto)15 User (com.networknt.portal.usermanagement.model.common.model.user.User)8 Map (java.util.Map)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 HttpString (io.undertow.util.HttpString)6 HashMap (java.util.HashMap)5 Http2Client (com.networknt.client.Http2Client)4 ApiException (com.networknt.exception.ApiException)4 ClientException (com.networknt.exception.ClientException)4 NoSuchUserException (com.networknt.portal.usermanagement.model.common.exception.NoSuchUserException)4 ClientConnection (io.undertow.client.ClientConnection)4 ClientRequest (io.undertow.client.ClientRequest)4 ClientResponse (io.undertow.client.ClientResponse)4 URI (java.net.URI)4 SQLException (java.sql.SQLException)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Test (org.junit.Test)4 Config (com.networknt.config.Config)2 EventHandlerMethod (com.networknt.eventuate.common.EventHandlerMethod)2