Search in sources :

Example 11 with RequestBody

use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project ezyfox-examples by tvd12.

the class AuthorController method addAuthor.

@DoPost("/add")
public AuthorResponse addAuthor(@RequestBody AddAuthorRequest request) {
    authorValidator.validate(request);
    final AddAuthorData addAuthorData = requestToDataConverter.toData(request);
    final AuthorData authorData = authorService.saveAuthor(addAuthorData);
    return dataToResponseConverter.toResponse(authorData);
}
Also used : AuthorData(com.tvd12.ezydata.example.jpa.data.AuthorData) AddAuthorData(com.tvd12.ezydata.example.jpa.data.AddAuthorData) AddAuthorData(com.tvd12.ezydata.example.jpa.data.AddAuthorData)

Example 12 with RequestBody

use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project ezyhttp by youngmonkeys.

the class HttpClient method request.

@SuppressWarnings("MethodLength")
public ResponseEntity request(HttpMethod method, String url, RequestEntity entity, Map<Integer, Class<?>> responseTypes, int connectTimeout, int readTimeout) throws Exception {
    if (url == null) {
        throw new IllegalArgumentException("url can not be null");
    }
    logger.debug("start: {} - {} - {}", method, url, entity != null ? entity.getHeaders() : null);
    HttpURLConnection connection = connect(url);
    try {
        connection.setConnectTimeout(connectTimeout > 0 ? connectTimeout : defaultConnectTimeout);
        connection.setReadTimeout(readTimeout > 0 ? readTimeout : defaultReadTimeout);
        connection.setRequestMethod(method.toString());
        connection.setDoInput(true);
        connection.setDoOutput(method.hasOutput());
        connection.setInstanceFollowRedirects(method == HttpMethod.GET);
        MultiValueMap requestHeaders = entity != null ? entity.getHeaders() : null;
        if (requestHeaders != null) {
            Map<String, String> encodedHeaders = requestHeaders.toMap();
            for (Entry<String, String> requestHeader : encodedHeaders.entrySet()) {
                connection.setRequestProperty(requestHeader.getKey(), requestHeader.getValue());
            }
        }
        Object requestBody = null;
        if (method != HttpMethod.GET && entity != null) {
            requestBody = entity.getBody();
        }
        byte[] requestBodyBytes = null;
        if (requestBody != null) {
            String requestContentType = connection.getRequestProperty(Headers.CONTENT_TYPE);
            if (requestContentType == null) {
                requestContentType = ContentTypes.APPLICATION_JSON;
                connection.setRequestProperty(Headers.CONTENT_TYPE, ContentTypes.APPLICATION_JSON);
            }
            requestBodyBytes = serializeRequestBody(requestContentType, requestBody);
            int requestContentLength = requestBodyBytes.length;
            connection.setFixedLengthStreamingMode(requestContentLength);
        }
        connection.connect();
        if (requestBodyBytes != null) {
            if (method.hasOutput()) {
                OutputStream outputStream = connection.getOutputStream();
                outputStream.write(requestBodyBytes);
                outputStream.flush();
                outputStream.close();
            } else {
                throw new IllegalArgumentException(method + " method can not have a payload body");
            }
        }
        int responseCode = connection.getResponseCode();
        Map<String, List<String>> headerFields = connection.getHeaderFields();
        MultiValueMap responseHeaders = MultiValueMap.of(headerFields);
        String responseContentType = responseHeaders.getValue(Headers.CONTENT_TYPE);
        if (responseContentType == null) {
            responseContentType = ContentTypes.APPLICATION_JSON;
        }
        InputStream inputStream = responseCode >= 400 ? connection.getErrorStream() : connection.getInputStream();
        Object responseBody = null;
        if (inputStream != null) {
            try {
                int responseContentLength = connection.getContentLength();
                Class<?> responseType = responseTypes.get(responseCode);
                responseBody = deserializeResponseBody(responseContentType, responseContentLength, inputStream, responseType);
            } finally {
                inputStream.close();
            }
        }
        logger.debug("end: {} - {} - {} - {}", method, url, responseCode, responseHeaders);
        return new ResponseEntity(responseCode, responseHeaders, responseBody);
    } finally {
        connection.disconnect();
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) HttpURLConnection(java.net.HttpURLConnection) ArrayList(java.util.ArrayList) List(java.util.List) MultiValueMap(com.tvd12.ezyhttp.core.data.MultiValueMap)

Example 13 with RequestBody

use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project ezyfox-examples by tvd12.

the class AuthorController method addAuthor.

@DoPost("/add")
public Author addAuthor(@RequestBody AddAuthorRequest request) {
    Author author = new Author(maxIdRepository.incrementAndGet("author"), request.getAuthorName());
    authorRepository.save(author);
    return author;
}
Also used : Author(com.tvd12.ezydata.example.mongo.entity.Author) DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost)

Example 14 with RequestBody

use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project ezyfox-examples by tvd12.

the class AuthorController method addAuthor.

@DoPost("/add")
public Author addAuthor(@RequestBody AddAuthorRequest request) {
    Author author = new Author(idGentor.incrementAndGet(), request.getAuthorName());
    authorMap.put(author.getId(), author);
    return author;
}
Also used : Author(com.tvd12.ezydata.example.redis.entity.Author) DoPost(com.tvd12.ezyhttp.server.core.annotation.DoPost)

Example 15 with RequestBody

use of com.tvd12.ezyhttp.server.core.annotation.RequestBody in project ezyfox-examples by tvd12.

the class FacebookService method getAccessToken.

@SuppressWarnings("unchecked")
@Override
public String getAccessToken(String code) {
    Map<String, Object> requestBody = EzyMapBuilder.mapBuilder().put("client_id", clientId).put("client_secret", clientSecret).put("redirect_uri", redirectUri).put("code", code).build();
    PostRequest request = new PostRequest().setURL(getTokenUri).setEntity(RequestEntity.builder().contentType(ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED).body(requestBody).build()).setResponseType(Map.class);
    try {
        Map<String, Object> response = httpClientProxy.call(request, DEFAULT_GOOGLE_AUTH_TIMEOUT);
        return (String) response.get("access_token");
    } catch (Exception e) {
        logger.info("get facebook access token error", e);
        return null;
    }
}
Also used : PostRequest(com.tvd12.ezyhttp.client.request.PostRequest)

Aggregations

DoPost (com.tvd12.ezyhttp.server.core.annotation.DoPost)6 HttpBadRequestException (com.tvd12.ezyhttp.core.exception.HttpBadRequestException)4 Author (com.tvd12.example.spring_boot_redis.entity.Author)2 Category (com.tvd12.example.spring_boot_redis.entity.Category)2 HttpBadRequestException (com.tvd12.example.spring_boot_redis.exception.HttpBadRequestException)2 Author (com.tvd12.ezydata.example.mongo.entity.Author)2 Category (com.tvd12.ezydata.example.mongo.entity.Category)2 Author (com.tvd12.ezydata.example.redis.entity.Author)2 Category (com.tvd12.ezydata.example.redis.entity.Category)2 EzyRedisAtomicLong (com.tvd12.ezydata.redis.EzyRedisAtomicLong)2 PostRequest (com.tvd12.ezyhttp.client.request.PostRequest)2 lombok.val (lombok.val)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 Book (com.tvd12.example.spring_boot_redis.entity.Book)1 BookNameAndAuthorId (com.tvd12.example.spring_boot_redis.entity.BookNameAndAuthorId)1 RedisAtomicLong (com.tvd12.example.spring_boot_redis.repository.RedisAtomicLong)1 AddAuthorData (com.tvd12.ezydata.example.jpa.data.AddAuthorData)1 AddBookData (com.tvd12.ezydata.example.jpa.data.AddBookData)1 AddCategoryData (com.tvd12.ezydata.example.jpa.data.AddCategoryData)1 AuthorData (com.tvd12.ezydata.example.jpa.data.AuthorData)1