Example usage for com.squareup.okhttp Response message

List of usage examples for com.squareup.okhttp Response message

Introduction

In this page you can find the example usage for com.squareup.okhttp Response message.

Prototype

String message

To view the source code for com.squareup.okhttp Response message.

Click Source Link

Usage

From source file:name.kevinlocke.appveyor.testutils.ConcurrentHttpLoggingInterceptor.java

License:Apache License

@Override
public Response intercept(Chain chain) throws IOException {
    Level level = this.level;

    Request request = chain.request();
    if (level == Level.NONE) {
        return chain.proceed(request);
    }//from  w ww .  j  av  a  2s . c  o  m

    boolean logBody = level == Level.BODY;
    boolean logHeaders = logBody || level == Level.HEADERS;

    RequestBody requestBody = request.body();

    Connection connection = chain.connection();
    Protocol protocol = connection != null ? connection.getProtocol() : Protocol.HTTP_1_1;
    UUID requestId = UUID.randomUUID();
    StringBuilder requestMessage = new StringBuilder("--> ").append(requestId).append('\n')
            .append(request.method()).append(' ').append(request.httpUrl()).append(' ').append(protocol);
    if (!logHeaders && requestBody != null) {
        requestMessage.append(" (").append(requestBody.contentLength()).append("-byte body)");
    }
    requestMessage.append('\n');

    if (logHeaders) {
        if (requestBody != null) {
            // Request body headers are only present when installed as a
            // network interceptor. Force
            // them to be included (when available) so there values are
            // known.
            if (requestBody.contentType() != null) {
                requestMessage.append("Content-Type: ").append(requestBody.contentType()).append('\n');
            }
            if (requestBody.contentLength() != -1) {
                requestMessage.append("Content-Length: ").append(requestBody.contentLength()).append('\n');
            }
        }

        Headers headers = request.headers();
        for (int i = 0, count = headers.size(); i < count; i++) {
            String name = headers.name(i);
            if ("Authorization".equalsIgnoreCase(name) || "Proxy-Authenticate".equalsIgnoreCase(name)
                    || "Proxy-Authorization".equalsIgnoreCase(name)
                    || "WWW-Authenticate".equalsIgnoreCase(name)) {
                requestMessage.append(name).append(": *****\n");
            }
            // Skip headers from the request body as they are explicitly
            // logged above.
            else if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
                requestMessage.append(name).append(": ").append(headers.value(i)).append('\n');
            }
        }

        if (!logBody || requestBody == null) {
            requestMessage.append("--> END ").append(requestId).append('\n');
        } else if (bodyEncoded(request.headers())) {
            requestMessage.append("--> END ").append(requestId).append(" (encoded body omitted)").append('\n');
        } else {
            Buffer buffer = new Buffer();
            requestBody.writeTo(buffer);

            Charset charset = UTF8;
            MediaType contentType = requestBody.contentType();
            if (contentType != null) {
                charset = contentType.charset(UTF8);
            }

            requestMessage.append('\n');
            if (isPlaintext(buffer)) {
                requestMessage.append(buffer.readString(charset)).append("\n--> END ").append(requestId)
                        .append(" (").append(requestBody.contentLength()).append("-byte body)\n");
            } else {
                requestMessage.append("--> END ").append(requestId).append(" (binary ")
                        .append(requestBody.contentLength()).append("-byte body omitted)\n");
            }
        }
    }

    logger.log(requestMessage.substring(0, requestMessage.length() - 1));

    long startNs = System.nanoTime();
    Response response;
    try {
        response = chain.proceed(request);
    } catch (Exception e) {
        logger.log("<-- " + requestId + "HTTP FAILED: " + e);
        throw e;
    }
    long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);

    ResponseBody responseBody = response.body();
    long contentLength = responseBody.contentLength();
    StringBuilder responseMessage = new StringBuilder("<-- ").append(requestId).append(' ')
            .append(response.request().url()).append(" (").append(tookMs).append("ms");
    if (!logHeaders) {
        responseMessage.append(", ");
        if (contentLength != -1) {
            responseMessage.append(contentLength).append("-byte");
        } else {
            responseMessage.append("unknown-length");
        }
        responseMessage.append(" body");
    }
    responseMessage.append(")\n");

    responseMessage.append(response.code()).append(' ').append(response.message()).append('\n');

    if (logHeaders) {
        Headers headers = response.headers();
        for (int i = 0, count = headers.size(); i < count; i++) {
            responseMessage.append(headers.name(i)).append(": ").append(headers.value(i)).append('\n');
        }

        if (!logBody || !HttpEngine.hasBody(response)) {
            responseMessage.append("<-- END HTTP\n");
        } else if (bodyEncoded(response.headers())) {
            responseMessage.append("<-- END HTTP (encoded body omitted)\n");
        } else {
            BufferedSource source = responseBody.source();
            source.request(Long.MAX_VALUE); // Buffer the entire body.
            Buffer buffer = source.buffer();

            Charset charset = UTF8;
            MediaType contentType = responseBody.contentType();
            if (contentType != null) {
                charset = contentType.charset(UTF8);
            }

            if (!isPlaintext(buffer)) {
                responseMessage.append('\n').append("<-- END HTTP (binary ").append(buffer.size())
                        .append("-byte body omitted)");
                logger.log(responseMessage.toString());
                return response;
            }

            if (contentLength != 0) {
                responseMessage.append('\n').append(buffer.clone().readString(charset)).append('\n');
            }

            responseMessage.append("<-- END HTTP (").append(buffer.size()).append("-byte body)\n");
        }
    }

    logger.log(responseMessage.substring(0, responseMessage.length() - 1));
    return response;
}

From source file:net.protyposis.android.mediaplayer.dash.DashMediaExtractor.java

License:Open Source License

/**
 * Blocking download of a segment.//  ww  w. java2 s .  c  om
 */
private CachedSegment downloadFile(Integer segmentNr) throws IOException {
    // At the first call, download the initialization segments, and reuse them later.
    if (mInitSegments.isEmpty()) {
        for (Representation representation : mAdaptationSet.representations) {
            Request request = buildSegmentRequest(representation.initSegment);
            long startTime = SystemClock.elapsedRealtime();
            Response response = mHttpClient.newCall(request).execute();
            if (!response.isSuccessful()) {
                throw new IOException("sync dl error @ init segment: " + response.code() + " "
                        + response.message() + " " + request.url().toString());
            }
            ByteString segmentData = response.body().source().readByteString();
            mInitSegments.put(representation, segmentData);
            mAdaptationLogic.reportSegmentDownload(mAdaptationSet, representation,
                    representation.segments.get(segmentNr), segmentData.size(),
                    SystemClock.elapsedRealtime() - startTime);
            Log.d(TAG, "init " + representation.initSegment.toString());
        }
    }

    Segment segment = mRepresentation.segments.get(segmentNr);
    Request request = buildSegmentRequest(segment);
    long startTime = SystemClock.elapsedRealtime();
    Response response = mHttpClient.newCall(request).execute();
    if (!response.isSuccessful()) {
        throw new IOException("sync dl error @ segment " + segmentNr + ": " + response.code() + " "
                + response.message() + " " + request.url().toString());
    }
    byte[] segmentData = response.body().bytes();
    mAdaptationLogic.reportSegmentDownload(mAdaptationSet, mRepresentation, segment, segmentData.length,
            SystemClock.elapsedRealtime() - startTime);
    CachedSegment cachedSegment = new CachedSegment(segmentNr, segment, mRepresentation);
    handleSegment(segmentData, cachedSegment);
    Log.d(TAG, "sync dl " + segmentNr + " " + segment.toString() + " -> " + cachedSegment.file.getPath());

    return cachedSegment;
}

From source file:org.apache.heron.scheduler.kubernetes.AppsV1beta1Controller.java

License:Apache License

@Override
boolean submit(PackingPlan packingPlan) {
    final String topologyName = getTopologyName();
    if (!topologyName.equals(topologyName.toLowerCase())) {
        throw new TopologySubmissionException("K8S scheduler does not allow upper case topologies.");
    }//from   w ww .j av  a2s  .  c  om

    final Resource containerResource = getContainerResource(packingPlan);

    // find the max number of instances in a container so we can open
    // enough ports if remote debugging is enabled.
    int numberOfInstances = 0;
    for (PackingPlan.ContainerPlan containerPlan : packingPlan.getContainers()) {
        numberOfInstances = Math.max(numberOfInstances, containerPlan.getInstances().size());
    }
    final V1beta1StatefulSet statefulSet = createStatefulSet(containerResource, numberOfInstances);

    try {
        final Response response = client
                .createNamespacedStatefulSetCall(getNamespace(), statefulSet, null, null, null).execute();
        if (!response.isSuccessful()) {
            LOG.log(Level.SEVERE, "Error creating topology message: " + response.message());
            KubernetesUtils.logResponseBodyIfPresent(LOG, response);
            // construct a message based on the k8s API server response
            throw new TopologySubmissionException(KubernetesUtils.errorMessageFromResponse(response));
        }
    } catch (IOException | ApiException e) {
        KubernetesUtils.logExceptionWithDetails(LOG, "Error creating topology", e);
        throw new TopologySubmissionException(e.getMessage());
    }

    return true;
}

From source file:org.apache.heron.scheduler.kubernetes.KubernetesUtils.java

License:Apache License

static String errorMessageFromResponse(Response response) {
    final String message = response.message();
    String details;//  w w w.  j a  v a  2 s .  co  m
    try {
        details = response.body().string();
    } catch (IOException ioe) {
        // ignore
        details = ioe.getMessage();
    } finally {
        SysUtils.closeIgnoringExceptions(response.body());
    }
    return message + "\ndetails:\n" + details;
}

From source file:org.apache.nifi.processors.att.m2x.GetM2XStream.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ProcessorLog logger = getLogger();
    final OkHttpClient httpClient = getHttpClient();
    final StateManager stateManager = context.getStateManager();
    final String apiKey = context.getProperty(M2X_API_KEY).getValue();
    final String apiUrl = context.getProperty(M2X_API_URL).getValue();
    final String deviceId = context.getProperty(M2X_DEVICE_ID).getValue();
    final String streamName = context.getProperty(M2X_STREAM_NAME).getValue();
    final String streamType = context.getProperty(M2X_STREAM_TYPE).getValue();
    final String startTime = getLastStartTime(context, stateManager);
    final String streamUrl = getStreamUrl(apiUrl, deviceId, streamName, startTime);

    String responseBody;//from  www .j av  a 2s  . c om
    try {
        final Request request = new Request.Builder().url(streamUrl).addHeader("X-M2X-KEY", apiKey).build();
        final Response response = httpClient.newCall(request).execute();

        if (!response.isSuccessful()) {
            logger.error(response.message());
            context.yield();
            return;
        }

        responseBody = response.body().string();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        context.yield();
        return;
    }

    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

    try {
        final M2XStreamValues m2xValues = mapper.readValue(responseBody, M2XStreamValues.class);
        final List<M2XStreamValue> m2xValueList = m2xValues.getValues();

        if (!CollectionUtils.isEmpty(m2xValueList)) {
            for (final M2XStreamValue m2xValue : m2xValueList) {
                final DateTime timestamp = m2xValue.getTimestamp();
                final Object valueObj = m2xValue.getValue();
                final Set<Map.Entry<String, Object>> properties = m2xValue.getAdditionalProperties().entrySet();
                final ByteArrayInputStream bytes = new ByteArrayInputStream(
                        String.valueOf(valueObj).getBytes(StandardCharsets.UTF_8));

                FlowFile newFlowFile = session.create();
                newFlowFile = session.importFrom(bytes, newFlowFile);
                newFlowFile = session.putAttribute(newFlowFile, "m2x.device.id", deviceId);
                newFlowFile = session.putAttribute(newFlowFile, "m2x.stream.name", streamName);
                newFlowFile = session.putAttribute(newFlowFile, "m2x.stream.start",
                        m2xValues.getStart().toString());
                newFlowFile = session.putAttribute(newFlowFile, "m2x.stream.end",
                        m2xValues.getEnd().toString());
                newFlowFile = session.putAttribute(newFlowFile, "m2x.stream.limit",
                        String.valueOf(m2xValues.getLimit()));
                newFlowFile = session.putAttribute(newFlowFile, "m2x.stream.value.timestamp",
                        timestamp.toString());
                newFlowFile = session.putAttribute(newFlowFile, "m2x.stream.value.millis",
                        String.valueOf(timestamp.getMillis()));
                for (final Map.Entry<String, Object> e : properties) {
                    newFlowFile = session.putAttribute(newFlowFile, "m2x.stream.value." + e.getKey(),
                            String.valueOf(e.getValue()));
                }

                session.getProvenanceReporter().create(newFlowFile);
                session.transfer(newFlowFile, REL_SUCCESS);
            }
        }

        setLastStartTime(stateManager, m2xValues.getEnd().toString());
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        context.yield();
    }
}

From source file:org.apache.nifi.processors.att.m2x.PutM2XStream.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;// w w w.j  ava2  s  .  c  om
    }

    final ProcessorLog logger = getLogger();
    final OkHttpClient httpClient = getHttpClient();
    final StateManager stateManager = context.getStateManager();
    final String apiKey = context.getProperty(M2X_API_KEY).getValue();
    final String apiUrl = context.getProperty(M2X_API_URL).getValue();
    final String deviceId = context.getProperty(M2X_DEVICE_ID).getValue();
    final String streamName = context.getProperty(M2X_STREAM_NAME).getValue();
    final String streamType = context.getProperty(M2X_STREAM_TYPE).getValue();
    final String streamUrl = new StringBuilder().append(apiUrl.replaceAll("/*$", "")).append("/devices/")
            .append(deviceId).append("/streams/").append(streamName).append("/value").toString();

    try {
        final AtomicReference<String> postBodyRef = new AtomicReference<>();
        session.read(flowFile, new InputStreamCallback() {
            @Override
            public void process(InputStream is) {
                try {
                    String timestamp = flowFile.getAttribute("m2x.stream.value.timestamp");
                    if (StringUtils.isEmpty(timestamp)) {
                        timestamp = ISODateTimeFormat.dateTime().print(flowFile.getEntryDate());
                    }
                    final String value = IOUtils.toString(is, StandardCharsets.UTF_8);

                    final M2XStreamValue m2xValue = new M2XStreamValue();
                    m2xValue.setValue(value);
                    m2xValue.setTimestamp(timestamp);

                    final ObjectMapper mapper = new ObjectMapper();
                    mapper.registerModule(new JodaModule());
                    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

                    final String postBody = mapper.writeValueAsString(m2xValue);
                    logger.warn("POST body is {}", new Object[] { postBody });
                    postBodyRef.set(postBody);
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
        });

        final String postBody = postBodyRef.get();
        if (StringUtils.isEmpty(postBody)) {
            logger.error("FlowFile {} contents didn't produce a valid M2X stream value",
                    new Object[] { flowFile });
            session.transfer(flowFile, REL_FAILURE);
            return;
        }

        final Request request = new Request.Builder().url(streamUrl).addHeader("X-M2X-KEY", apiKey)
                .put(RequestBody.create(MEDIA_TYPE_JSON, postBody)).build();
        final Response response = httpClient.newCall(request).execute();

        if (!response.isSuccessful()) {
            logger.error(response.message());
            context.yield();
            session.penalize(flowFile);
            return;
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        context.yield();
        session.penalize(flowFile);
        return;
    }

    session.transfer(flowFile, REL_SUCCESS);
}

From source file:org.apache.nifi.processors.standard.InvokeHTTP.java

License:Apache License

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    OkHttpClient okHttpClient = okHttpClientAtomicReference.get();

    FlowFile requestFlowFile = session.get();

    // Checking to see if the property to put the body of the response in an attribute was set
    boolean putToAttribute = context.getProperty(PROP_PUT_OUTPUT_IN_ATTRIBUTE).isSet();
    if (requestFlowFile == null) {
        if (context.hasNonLoopConnection()) {
            return;
        }//  w  w w  . j  a  v a 2  s  .c  o m

        String request = context.getProperty(PROP_METHOD).evaluateAttributeExpressions().getValue()
                .toUpperCase();
        if ("POST".equals(request) || "PUT".equals(request) || "PATCH".equals(request)) {
            return;
        } else if (putToAttribute) {
            requestFlowFile = session.create();
        }
    }

    // Setting some initial variables
    final int maxAttributeSize = context.getProperty(PROP_PUT_ATTRIBUTE_MAX_LENGTH).asInteger();
    final ComponentLog logger = getLogger();

    // Every request/response cycle has a unique transaction id which will be stored as a flowfile attribute.
    final UUID txId = UUID.randomUUID();

    FlowFile responseFlowFile = null;
    try {
        // read the url property from the context
        final String urlstr = trimToEmpty(
                context.getProperty(PROP_URL).evaluateAttributeExpressions(requestFlowFile).getValue());
        final URL url = new URL(urlstr);

        Request httpRequest = configureRequest(context, session, requestFlowFile, url);

        // log request
        logRequest(logger, httpRequest);

        // emit send provenance event if successfully sent to the server
        if (httpRequest.body() != null) {
            session.getProvenanceReporter().send(requestFlowFile, url.toExternalForm(), true);
        }

        final long startNanos = System.nanoTime();
        Response responseHttp = okHttpClient.newCall(httpRequest).execute();

        // output the raw response headers (DEBUG level only)
        logResponse(logger, url, responseHttp);

        // store the status code and message
        int statusCode = responseHttp.code();
        String statusMessage = responseHttp.message();

        if (statusCode == 0) {
            throw new IllegalStateException("Status code unknown, connection hasn't been attempted.");
        }

        // Create a map of the status attributes that are always written to the request and response FlowFiles
        Map<String, String> statusAttributes = new HashMap<>();
        statusAttributes.put(STATUS_CODE, String.valueOf(statusCode));
        statusAttributes.put(STATUS_MESSAGE, statusMessage);
        statusAttributes.put(REQUEST_URL, url.toExternalForm());
        statusAttributes.put(TRANSACTION_ID, txId.toString());

        if (requestFlowFile != null) {
            requestFlowFile = session.putAllAttributes(requestFlowFile, statusAttributes);
        }

        // If the property to add the response headers to the request flowfile is true then add them
        if (context.getProperty(PROP_ADD_HEADERS_TO_REQUEST).asBoolean() && requestFlowFile != null) {
            // write the response headers as attributes
            // this will overwrite any existing flowfile attributes
            requestFlowFile = session.putAllAttributes(requestFlowFile,
                    convertAttributesFromHeaders(url, responseHttp));
        }

        boolean outputBodyToRequestAttribute = (!isSuccess(statusCode) || putToAttribute)
                && requestFlowFile != null;
        boolean outputBodyToResponseContent = (isSuccess(statusCode) && !putToAttribute)
                || context.getProperty(PROP_OUTPUT_RESPONSE_REGARDLESS).asBoolean();
        ResponseBody responseBody = responseHttp.body();
        boolean bodyExists = responseBody != null;

        InputStream responseBodyStream = null;
        SoftLimitBoundedByteArrayOutputStream outputStreamToRequestAttribute = null;
        TeeInputStream teeInputStream = null;
        try {
            responseBodyStream = bodyExists ? responseBody.byteStream() : null;
            if (responseBodyStream != null && outputBodyToRequestAttribute && outputBodyToResponseContent) {
                outputStreamToRequestAttribute = new SoftLimitBoundedByteArrayOutputStream(maxAttributeSize);
                teeInputStream = new TeeInputStream(responseBodyStream, outputStreamToRequestAttribute);
            }

            if (outputBodyToResponseContent) {
                /*
                 * If successful and putting to response flowfile, store the response body as the flowfile payload
                 * we include additional flowfile attributes including the response headers and the status codes.
                 */

                // clone the flowfile to capture the response
                if (requestFlowFile != null) {
                    responseFlowFile = session.create(requestFlowFile);
                } else {
                    responseFlowFile = session.create();
                }

                // write attributes to response flowfile
                responseFlowFile = session.putAllAttributes(responseFlowFile, statusAttributes);

                // write the response headers as attributes
                // this will overwrite any existing flowfile attributes
                responseFlowFile = session.putAllAttributes(responseFlowFile,
                        convertAttributesFromHeaders(url, responseHttp));

                // transfer the message body to the payload
                // can potentially be null in edge cases
                if (bodyExists) {
                    // write content type attribute to response flowfile if it is available
                    if (responseBody.contentType() != null) {
                        responseFlowFile = session.putAttribute(responseFlowFile,
                                CoreAttributes.MIME_TYPE.key(), responseBody.contentType().toString());
                    }
                    if (teeInputStream != null) {
                        responseFlowFile = session.importFrom(teeInputStream, responseFlowFile);
                    } else {
                        responseFlowFile = session.importFrom(responseBodyStream, responseFlowFile);
                    }

                    // emit provenance event
                    final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
                    if (requestFlowFile != null) {
                        session.getProvenanceReporter().fetch(responseFlowFile, url.toExternalForm(), millis);
                    } else {
                        session.getProvenanceReporter().receive(responseFlowFile, url.toExternalForm(), millis);
                    }
                }
            }

            // if not successful and request flowfile is not null, store the response body into a flowfile attribute
            if (outputBodyToRequestAttribute && bodyExists) {
                String attributeKey = context.getProperty(PROP_PUT_OUTPUT_IN_ATTRIBUTE)
                        .evaluateAttributeExpressions(requestFlowFile).getValue();
                if (attributeKey == null) {
                    attributeKey = RESPONSE_BODY;
                }
                byte[] outputBuffer;
                int size;

                if (outputStreamToRequestAttribute != null) {
                    outputBuffer = outputStreamToRequestAttribute.getBuffer();
                    size = outputStreamToRequestAttribute.size();
                } else {
                    outputBuffer = new byte[maxAttributeSize];
                    size = StreamUtils.fillBuffer(responseBodyStream, outputBuffer, false);
                }
                String bodyString = new String(outputBuffer, 0, size,
                        getCharsetFromMediaType(responseBody.contentType()));
                requestFlowFile = session.putAttribute(requestFlowFile, attributeKey, bodyString);

                final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
                session.getProvenanceReporter().modifyAttributes(requestFlowFile,
                        "The " + attributeKey
                                + " has been added. The value of which is the body of a http call to "
                                + url.toExternalForm() + ". It took " + millis + "millis,");
            }
        } finally {
            if (outputStreamToRequestAttribute != null) {
                outputStreamToRequestAttribute.close();
                outputStreamToRequestAttribute = null;
            }
            if (teeInputStream != null) {
                teeInputStream.close();
                teeInputStream = null;
            } else if (responseBodyStream != null) {
                responseBodyStream.close();
                responseBodyStream = null;
            }
        }

        route(requestFlowFile, responseFlowFile, session, context, statusCode);
    } catch (final Exception e) {
        // penalize or yield
        if (requestFlowFile != null) {
            logger.error("Routing to {} due to exception: {}", new Object[] { REL_FAILURE.getName(), e }, e);
            requestFlowFile = session.penalize(requestFlowFile);
            requestFlowFile = session.putAttribute(requestFlowFile, EXCEPTION_CLASS, e.getClass().getName());
            requestFlowFile = session.putAttribute(requestFlowFile, EXCEPTION_MESSAGE, e.getMessage());
            // transfer original to failure
            session.transfer(requestFlowFile, REL_FAILURE);
        } else {
            logger.error("Yielding processor due to exception encountered as a source processor: {}", e);
            context.yield();
        }

        // cleanup response flowfile, if applicable
        try {
            if (responseFlowFile != null) {
                session.remove(responseFlowFile);
            }
        } catch (final Exception e1) {
            logger.error("Could not cleanup response flowfile due to exception: {}", new Object[] { e1 }, e1);
        }
    }
}

From source file:org.apache.pulsar.functions.runtime.KubernetesRuntime.java

License:Apache License

public void deleteStatefulSet() throws InterruptedException {
    String statefulSetName = createJobName(instanceConfig.getFunctionDetails());
    final V1DeleteOptions options = new V1DeleteOptions();
    options.setGracePeriodSeconds(5L);/*from  w  w w.j  ava2s .  c o  m*/
    options.setPropagationPolicy("Foreground");

    String fqfn = FunctionDetailsUtils.getFullyQualifiedName(instanceConfig.getFunctionDetails());
    RuntimeUtils.Actions.Action deleteStatefulSet = RuntimeUtils.Actions.Action.builder()
            .actionName(String.format("Deleting statefulset for function %s", fqfn)).numRetries(NUM_RETRIES)
            .sleepBetweenInvocationsMs(SLEEP_BETWEEN_RETRIES_MS).supplier(() -> {
                Response response;
                try {
                    // cannot use deleteNamespacedStatefulSet because of bug in kuberenetes
                    // https://github.com/kubernetes-client/java/issues/86
                    response = appsClient.deleteNamespacedStatefulSetCall(statefulSetName, jobNamespace,
                            options, null, null, null, null, null, null).execute();
                } catch (ApiException e) {
                    // if already deleted
                    if (e.getCode() == HTTP_NOT_FOUND) {
                        log.warn("Statefulset for function {} does not exist", fqfn);
                        return RuntimeUtils.Actions.ActionResult.builder().success(true).build();
                    }

                    String errorMsg = e.getResponseBody() != null ? e.getResponseBody() : e.getMessage();
                    return RuntimeUtils.Actions.ActionResult.builder().success(false).errorMsg(errorMsg)
                            .build();
                } catch (IOException e) {
                    return RuntimeUtils.Actions.ActionResult.builder().success(false).errorMsg(e.getMessage())
                            .build();
                }

                // if already deleted
                if (response.code() == HTTP_NOT_FOUND) {
                    log.warn("Statefulset for function {} does not exist", fqfn);
                    return RuntimeUtils.Actions.ActionResult.builder().success(true).build();
                } else {
                    return RuntimeUtils.Actions.ActionResult.builder().success(response.isSuccessful())
                            .errorMsg(response.message()).build();
                }
            }).build();

    RuntimeUtils.Actions.Action waitForStatefulSetDeletion = RuntimeUtils.Actions.Action.builder()
            .actionName(String.format("Waiting for statefulset for function %s to complete deletion", fqfn))
            // set retry period to be about 2x the graceshutdown time
            .numRetries(NUM_RETRIES * 2).sleepBetweenInvocationsMs(SLEEP_BETWEEN_RETRIES_MS * 2)
            .supplier(() -> {
                V1StatefulSet response;
                try {
                    response = appsClient.readNamespacedStatefulSet(statefulSetName, jobNamespace, null, null,
                            null);
                } catch (ApiException e) {
                    // statefulset is gone
                    if (e.getCode() == HTTP_NOT_FOUND) {
                        return RuntimeUtils.Actions.ActionResult.builder().success(true).build();
                    }

                    String errorMsg = e.getResponseBody() != null ? e.getResponseBody() : e.getMessage();
                    return RuntimeUtils.Actions.ActionResult.builder().success(false).errorMsg(errorMsg)
                            .build();
                }
                return RuntimeUtils.Actions.ActionResult.builder().success(false)
                        .errorMsg(response.getStatus().toString()).build();
            }).build();

    // Need to wait for all pods to die so we can cleanup subscriptions.
    RuntimeUtils.Actions.Action waitForStatefulPodsToTerminate = RuntimeUtils.Actions.Action.builder()
            .actionName(String.format("Waiting for pods for function %s to terminate", fqfn))
            .numRetries(NUM_RETRIES * 2).sleepBetweenInvocationsMs(SLEEP_BETWEEN_RETRIES_MS * 2)
            .supplier(() -> {
                String labels = String.format("tenant=%s,namespace=%s,name=%s",
                        instanceConfig.getFunctionDetails().getTenant(),
                        instanceConfig.getFunctionDetails().getNamespace(),
                        instanceConfig.getFunctionDetails().getName());

                V1PodList response;
                try {
                    response = coreClient.listNamespacedPod(jobNamespace, null, null, null, null, labels, null,
                            null, null, null);
                } catch (ApiException e) {

                    String errorMsg = e.getResponseBody() != null ? e.getResponseBody() : e.getMessage();
                    return RuntimeUtils.Actions.ActionResult.builder().success(false).errorMsg(errorMsg)
                            .build();
                }

                if (response.getItems().size() > 0) {
                    return RuntimeUtils.Actions.ActionResult.builder().success(false)
                            .errorMsg(response.getItems().size() + " pods still alive.").build();
                } else {
                    return RuntimeUtils.Actions.ActionResult.builder().success(true).build();
                }
            }).build();

    AtomicBoolean success = new AtomicBoolean(false);
    RuntimeUtils.Actions.newBuilder().addAction(deleteStatefulSet.toBuilder().continueOn(true).build())
            .addAction(waitForStatefulSetDeletion.toBuilder().continueOn(false)
                    .onSuccess(() -> success.set(true)).build())
            .addAction(deleteStatefulSet.toBuilder().continueOn(true).build())
            .addAction(waitForStatefulSetDeletion.toBuilder().onSuccess(() -> success.set(true)).build()).run();

    if (!success.get()) {
        throw new RuntimeException(String.format("Failed to delete statefulset for function %s", fqfn));
    } else {
        // wait for pods to terminate
        RuntimeUtils.Actions.newBuilder().addAction(waitForStatefulPodsToTerminate).run();
    }
}

From source file:org.apache.pulsar.functions.runtime.KubernetesRuntime.java

License:Apache License

public void deleteService() throws InterruptedException {

    final V1DeleteOptions options = new V1DeleteOptions();
    options.setGracePeriodSeconds(0L);/*w  w  w.j a  v a 2  s.c om*/
    options.setPropagationPolicy("Foreground");
    String fqfn = FunctionDetailsUtils.getFullyQualifiedName(instanceConfig.getFunctionDetails());
    String serviceName = createJobName(instanceConfig.getFunctionDetails());

    RuntimeUtils.Actions.Action deleteService = RuntimeUtils.Actions.Action.builder()
            .actionName(String.format("Deleting service for function %s", fqfn)).numRetries(NUM_RETRIES)
            .sleepBetweenInvocationsMs(SLEEP_BETWEEN_RETRIES_MS).supplier(() -> {
                final Response response;
                try {
                    // cannot use deleteNamespacedService because of bug in kuberenetes
                    // https://github.com/kubernetes-client/java/issues/86
                    response = coreClient.deleteNamespacedServiceCall(serviceName, jobNamespace, options, null,
                            null, null, null, null, null).execute();
                } catch (ApiException e) {
                    // if already deleted
                    if (e.getCode() == HTTP_NOT_FOUND) {
                        log.warn("Service for function {} does not exist", fqfn);
                        return RuntimeUtils.Actions.ActionResult.builder().success(true).build();
                    }

                    String errorMsg = e.getResponseBody() != null ? e.getResponseBody() : e.getMessage();
                    return RuntimeUtils.Actions.ActionResult.builder().success(false).errorMsg(errorMsg)
                            .build();
                } catch (IOException e) {
                    return RuntimeUtils.Actions.ActionResult.builder().success(false).errorMsg(e.getMessage())
                            .build();
                }

                // if already deleted
                if (response.code() == HTTP_NOT_FOUND) {
                    log.warn("Service for function {} does not exist", fqfn);
                    return RuntimeUtils.Actions.ActionResult.builder().success(true).build();
                } else {
                    return RuntimeUtils.Actions.ActionResult.builder().success(response.isSuccessful())
                            .errorMsg(response.message()).build();
                }
            }).build();

    RuntimeUtils.Actions.Action waitForServiceDeletion = RuntimeUtils.Actions.Action.builder()
            .actionName(String.format("Waiting for statefulset for function %s to complete deletion", fqfn))
            .numRetries(NUM_RETRIES).sleepBetweenInvocationsMs(SLEEP_BETWEEN_RETRIES_MS).supplier(() -> {
                V1Service response;
                try {
                    response = coreClient.readNamespacedService(serviceName, jobNamespace, null, null, null);

                } catch (ApiException e) {
                    // statefulset is gone
                    if (e.getCode() == HTTP_NOT_FOUND) {
                        return RuntimeUtils.Actions.ActionResult.builder().success(true).build();
                    }
                    String errorMsg = e.getResponseBody() != null ? e.getResponseBody() : e.getMessage();
                    return RuntimeUtils.Actions.ActionResult.builder().success(false).errorMsg(errorMsg)
                            .build();
                }
                return RuntimeUtils.Actions.ActionResult.builder().success(false)
                        .errorMsg(response.getStatus().toString()).build();
            }).build();

    AtomicBoolean success = new AtomicBoolean(false);
    RuntimeUtils.Actions.newBuilder().addAction(deleteService.toBuilder().continueOn(true).build())
            .addAction(waitForServiceDeletion.toBuilder().continueOn(false).onSuccess(() -> success.set(true))
                    .build())
            .addAction(deleteService.toBuilder().continueOn(true).build())
            .addAction(waitForServiceDeletion.toBuilder().onSuccess(() -> success.set(true)).build()).run();

    if (!success.get()) {
        throw new RuntimeException(String.format("Failed to delete service for function %s", fqfn));
    }
}

From source file:org.bitcoinj_extra.net.discovery.HttpDiscovery.java

License:Apache License

@Override
public InetSocketAddress[] getPeers(long services, long timeoutValue, TimeUnit timeoutUnit)
        throws PeerDiscoveryException {
    try {/*from  w w  w .  j a  va 2 s.  com*/
        HttpUrl.Builder url = HttpUrl.get(details.uri).newBuilder();
        if (services != 0)
            url.addQueryParameter("srvmask", Long.toString(services));
        Request.Builder request = new Request.Builder();
        request.url(url.build());
        request.addHeader("User-Agent", VersionMessage.LIBRARY_SUBVER); // TODO Add main version.
        log.info("Requesting seeds from {}", url);
        Response response = client.newCall(request.build()).execute();
        if (!response.isSuccessful())
            throw new PeerDiscoveryException(
                    "HTTP request failed: " + response.code() + " " + response.message());
        InputStream stream = response.body().byteStream();
        GZIPInputStream zip = new GZIPInputStream(stream);
        PeerSeedProtos.SignedPeerSeeds proto = PeerSeedProtos.SignedPeerSeeds.parseDelimitedFrom(zip);
        stream.close();
        return protoToAddrs(proto);
    } catch (PeerDiscoveryException e1) {
        throw e1;
    } catch (Exception e) {
        throw new PeerDiscoveryException(e);
    }
}