Search in sources :

Example 21 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project nightfall-java-sdk by nightfallai.

the class NightfallClient method scanUploadedFile.

/**
 * Triggers a scan of the file identified by the provided <code>fileID</code>. As the underlying file might be
 * arbitrarily large, this scan will be conducted asynchronously. Results from the scan will be delivered to the
 * webhook URL provided in the <code>request</code> payload.
 *
 * @param request contains metadata identifying which file to scan, as well as the configuration that
 *                describes which detectors to use when scanning.
 * @return an acknowledgment that the asynchronous scan has been initiated.
 * @throws NightfallAPIException thrown if a non-2xx status code is returned by the API.
 * @throws NightfallClientException thrown if a I/O error occurs while processing the request
 * @throws NightfallRequestTimeoutException thrown if the request is aborted because read/write timeout is exceeded
 */
private ScanFileResponse scanUploadedFile(ScanFileRequest request, UUID fileID) {
    String path = "/v3/upload/" + fileID.toString() + "/scan";
    byte[] jsonBody;
    try {
        jsonBody = objectMapper.writeValueAsBytes(request);
    } catch (JsonProcessingException e) {
        throw new NightfallClientException("processing scan file request: " + e.getMessage());
    }
    MediaType json = MediaType.parse("application/json");
    return this.issueRequest(path, "POST", json, jsonBody, null, ScanFileResponse.class);
}
Also used : NightfallClientException(ai.nightfall.scan.model.NightfallClientException) MediaType(okhttp3.MediaType) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 22 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project nightfall-java-sdk by nightfallai.

the class NightfallClient method scanText.

/**
 * Scans the provided plaintext against the provided detectors, and returns all findings. The response object will
 * contain a list of lists representing the findings. Each index <code>i</code> in the findings array will
 * correspond one-to-one with the input request payload list, so all findings stored in a given sub-list refer to
 * matches that occurred in the <code>i</code>th index of the request payload.
 *
 * @param request the data to scan, along with the configuration describing how to scan the data. The
 *                request payload may not exceed 500KB.
 * @return an object containing the findings from each item in the request payload
 * @throws NightfallAPIException thrown if a non-2xx status code is returned by the API.
 * @throws NightfallClientException thrown if a I/O error occurs while processing the request
 * @throws IllegalArgumentException thrown if <code>request</code> is null
 * @throws NightfallRequestTimeoutException thrown if the request is aborted because the timeout is exceeded
 */
public ScanTextResponse scanText(ScanTextRequest request) {
    if (request == null) {
        throw new IllegalArgumentException("request must be non-null");
    }
    byte[] jsonBody;
    try {
        jsonBody = objectMapper.writeValueAsBytes(request);
    } catch (JsonProcessingException e) {
        throw new NightfallClientException("processing scan request: " + e.getMessage());
    }
    MediaType json = MediaType.parse("application/json");
    return this.issueRequest("/v3/scan", "POST", json, jsonBody, null, ScanTextResponse.class);
}
Also used : NightfallClientException(ai.nightfall.scan.model.NightfallClientException) MediaType(okhttp3.MediaType) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 23 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project nightfall-java-sdk by nightfallai.

the class NightfallClient method initializeFileUpload.

/**
 * Creates a file upload session. If this operation returns successfully, the ID returned as part of the
 * response object shall be used to refer to the file in all subsequent upload and scanning operations.
 *
 * @param request contains metadata describing the requested file upload, such as the file size in bytes.
 * @return an object representing the file upload.
 * @throws NightfallAPIException thrown if a non-2xx status code is returned by the API.
 * @throws NightfallClientException thrown if a I/O error occurs while processing the request
 * @throws NightfallRequestTimeoutException thrown if the request is aborted because read/write timeout is exceeded
 */
private FileUpload initializeFileUpload(InitializeFileUploadRequest request) {
    byte[] jsonBody;
    try {
        jsonBody = objectMapper.writeValueAsBytes(request);
    } catch (JsonProcessingException e) {
        throw new NightfallClientException("processing init-upload request: " + e.getMessage());
    }
    MediaType json = MediaType.parse("application/json");
    return this.issueRequest("/v3/upload", "POST", json, jsonBody, null, FileUpload.class);
}
Also used : NightfallClientException(ai.nightfall.scan.model.NightfallClientException) MediaType(okhttp3.MediaType) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 24 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project nightfall-java-sdk by nightfallai.

the class NightfallClient method uploadFileChunk.

/**
 * Uploads the bytes stored at the provided offset of a file. The byte offset provided should be an exact
 * multiple of the <code>chunkSize</code> returned by the response when the upload session was created. The
 * number of bytes provided in the request should exactly match <code>chunkSize</code>, except if this chunk is
 * the last chunk of the file; then it may be less.
 *
 * @param request the data to upload, as well as metadata such as the offset at which to upload.
 * @return true if the chunk was uploaded
 * @throws NightfallAPIException thrown if a non-2xx status code is returned by the API.
 * @throws NightfallClientException thrown if a I/O error occurs while processing the request
 * @throws NightfallRequestTimeoutException thrown if the request is aborted because read/write timeout is exceeded
 */
private boolean uploadFileChunk(UploadFileChunkRequest request) {
    Headers headers = Headers.of("X-Upload-Offset", Long.toString(request.getFileOffset()));
    String path = "/v3/upload/" + request.getFileUploadID().toString();
    MediaType octetStream = MediaType.parse("application/octet-stream");
    this.issueRequest(path, "PATCH", octetStream, request.getContent(), headers, Void.class);
    return true;
}
Also used : Headers(okhttp3.Headers) MediaType(okhttp3.MediaType)

Example 25 with MediaType

use of com.reprezen.kaizen.oasparser.model3.MediaType in project retrofit-spring-boot-starter by LianjiaTech.

the class RetrofitUtils method readResponseBody.

/**
 * read ResponseBody as String
 *
 * @param response response
 * @return ResponseBody String
 * @throws IOException
 */
public static String readResponseBody(Response response) throws ReadResponseBodyException {
    try {
        Headers headers = response.headers();
        if (bodyHasUnknownEncoding(headers)) {
            return null;
        }
        ResponseBody responseBody = response.body();
        if (responseBody == null) {
            return null;
        }
        long contentLength = responseBody.contentLength();
        BufferedSource source = responseBody.source();
        // Buffer the entire body.
        source.request(Long.MAX_VALUE);
        Buffer buffer = source.getBuffer();
        if (GZIP.equalsIgnoreCase(headers.get(CONTENT_ENCODING))) {
            try (GzipSource gzippedResponseBody = new GzipSource(buffer.clone())) {
                buffer = new Buffer();
                buffer.writeAll(gzippedResponseBody);
            }
        }
        Charset charset = UTF8;
        MediaType contentType = responseBody.contentType();
        if (contentType != null) {
            charset = contentType.charset(UTF8);
        }
        if (contentLength != 0) {
            return buffer.clone().readString(charset);
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new ReadResponseBodyException(e);
    }
}
Also used : Buffer(okio.Buffer) GzipSource(okio.GzipSource) ReadResponseBodyException(com.github.lianjiatech.retrofit.spring.boot.exception.ReadResponseBodyException) Headers(okhttp3.Headers) Charset(java.nio.charset.Charset) MediaType(okhttp3.MediaType) IOException(java.io.IOException) ReadResponseBodyException(com.github.lianjiatech.retrofit.spring.boot.exception.ReadResponseBodyException) ResponseBody(okhttp3.ResponseBody) BufferedSource(okio.BufferedSource)

Aggregations

MediaType (okhttp3.MediaType)297 RequestBody (okhttp3.RequestBody)186 Request (okhttp3.Request)179 Response (okhttp3.Response)158 IOException (java.io.IOException)137 ResponseBody (okhttp3.ResponseBody)71 OkHttpClient (okhttp3.OkHttpClient)68 Charset (java.nio.charset.Charset)50 Buffer (okio.Buffer)50 Headers (okhttp3.Headers)48 JSONObject (org.json.JSONObject)38 BufferedSource (okio.BufferedSource)36 MultipartBody (okhttp3.MultipartBody)34 HttpUrl (okhttp3.HttpUrl)30 Map (java.util.Map)24 BufferedSink (okio.BufferedSink)23 File (java.io.File)22 InputStream (java.io.InputStream)21 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21