Search in sources :

Example 1 with BinaryData

use of com.stanfy.enroscar.rest.request.binary.BinaryData in project enroscar by stanfy.

the class UploadPostConverter method composeParts.

/**
   * @param context application context
   * @param requestDescription request description
   * @return upload request parts obtained from the request description
   * @throws IOException if an I/O error happens
   */
protected static Part[] composeParts(final Context context, final RequestDescription requestDescription) throws IOException {
    final ArrayList<BinaryData<?>> binaryData = requestDescription.getBinaryData();
    final List<Parameter> params = requestDescription.getSimpleParameters().getChildren();
    int realCount = 0;
    final int binaryCount = binaryData != null ? binaryData.size() : 0;
    Part[] parts = new Part[params.size() + binaryCount];
    for (final Parameter p : params) {
        if (p instanceof ParameterValue) {
            final ParameterValue pv = (ParameterValue) p;
            if (pv.getValue() == null) {
                continue;
            }
            parts[realCount++] = new StringPart(pv.getName(), pv.getValue(), requestDescription.getEncoding().name());
        }
    }
    for (int i = 0; i < binaryCount; i++) {
        final Part part = binaryData.get(i).createHttpPart(context);
        if (part != null) {
            parts[realCount++] = part;
        }
    }
    if (realCount < parts.length) {
        final Part[] trim = new Part[realCount];
        System.arraycopy(parts, 0, trim, 0, realCount);
        parts = trim;
    }
    return parts;
}
Also used : ParameterValue(com.stanfy.enroscar.net.operation.ParameterValue) Part(com.stanfy.enroscar.rest.request.net.multipart.Part) StringPart(com.stanfy.enroscar.rest.request.net.multipart.StringPart) StringPart(com.stanfy.enroscar.rest.request.net.multipart.StringPart) Parameter(com.stanfy.enroscar.net.operation.Parameter) BinaryData(com.stanfy.enroscar.rest.request.binary.BinaryData)

Example 2 with BinaryData

use of com.stanfy.enroscar.rest.request.binary.BinaryData in project enroscar by stanfy.

the class PayloadPostConverter method sendRequest.

@Override
public void sendRequest(final URLConnection connection) throws IOException {
    ArrayList<BinaryData<?>> binaryData = getRequestDescription().getBinaryData();
    if (binaryData != null) {
        final BuffersPool buffersPool = BeansManager.get(getContext()).getContainer().getBean(BuffersPool.class);
        final OutputStream out = buffersPool.bufferize(connection.getOutputStream());
        try {
            int size = binaryData.size();
            //noinspection ForLoopReplaceableByForEach
            for (int i = 0; i < size; i++) {
                binaryData.get(i).writeContentTo(getContext(), out);
            }
            doSendWorkarounds(UrlConnectionWrapper.unwrap(connection));
        } finally {
            IoUtils.closeQuietly(out);
        }
    }
}
Also used : BuffersPool(com.stanfy.enroscar.io.BuffersPool) OutputStream(java.io.OutputStream) BinaryData(com.stanfy.enroscar.rest.request.binary.BinaryData)

Aggregations

BinaryData (com.stanfy.enroscar.rest.request.binary.BinaryData)2 BuffersPool (com.stanfy.enroscar.io.BuffersPool)1 Parameter (com.stanfy.enroscar.net.operation.Parameter)1 ParameterValue (com.stanfy.enroscar.net.operation.ParameterValue)1 Part (com.stanfy.enroscar.rest.request.net.multipart.Part)1 StringPart (com.stanfy.enroscar.rest.request.net.multipart.StringPart)1 OutputStream (java.io.OutputStream)1