Search in sources :

Example 91 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project nifi by apache.

the class ClusterReplicationComponentLifecycle method waitForControllerServiceStatus.

/**
 * Periodically polls the process group with the given ID, waiting for all controller services whose ID's are given to have the given Controller Service State.
 *
 * @param user the user making the request
 * @param groupId the ID of the Process Group to poll
 * @param serviceIds the ID of all Controller Services whose state should be equal to the given desired state
 * @param desiredState the desired state for all services with the ID's given
 * @param pause the Pause that can be used to wait between polling
 * @return <code>true</code> if successful, <code>false</code> if unable to wait for services to reach the desired state
 */
private boolean waitForControllerServiceStatus(final NiFiUser user, final URI originalUri, final String groupId, final Set<String> serviceIds, final ControllerServiceState desiredState, final Pause pause) throws InterruptedException {
    URI groupUri;
    try {
        groupUri = new URI(originalUri.getScheme(), originalUri.getUserInfo(), originalUri.getHost(), originalUri.getPort(), "/nifi-api/flow/process-groups/" + groupId + "/controller-services", "includeAncestorGroups=false,includeDescendantGroups=true", originalUri.getFragment());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
    final Map<String, String> headers = new HashMap<>();
    final MultivaluedMap<String, String> requestEntity = new MultivaluedHashMap<>();
    boolean continuePolling = true;
    while (continuePolling) {
        // Determine whether we should replicate only to the cluster coordinator, or if we should replicate directly to the cluster nodes themselves.
        final NodeResponse clusterResponse;
        if (getReplicationTarget() == ReplicationTarget.CLUSTER_NODES) {
            clusterResponse = getRequestReplicator().replicate(user, HttpMethod.GET, groupUri, requestEntity, headers).awaitMergedResponse();
        } else {
            clusterResponse = getRequestReplicator().forwardToCoordinator(getClusterCoordinatorNode(), user, HttpMethod.GET, groupUri, requestEntity, headers).awaitMergedResponse();
        }
        if (clusterResponse.getStatus() != Status.OK.getStatusCode()) {
            return false;
        }
        final ControllerServicesEntity controllerServicesEntity = getResponseEntity(clusterResponse, ControllerServicesEntity.class);
        final Set<ControllerServiceEntity> serviceEntities = controllerServicesEntity.getControllerServices();
        final Map<String, AffectedComponentEntity> affectedServices = serviceEntities.stream().collect(Collectors.toMap(ControllerServiceEntity::getId, dtoFactory::createAffectedComponentEntity));
        // update the affected controller services
        updateAffectedControllerServices(serviceEntities, affectedServices);
        final String desiredStateName = desiredState.name();
        final boolean allServicesMatch = serviceEntities.stream().map(entity -> entity.getComponent()).filter(service -> serviceIds.contains(service.getId())).map(service -> service.getState()).allMatch(state -> state.equals(desiredStateName));
        if (allServicesMatch) {
            logger.debug("All {} controller services of interest now have the desired state of {}", serviceIds.size(), desiredState);
            return true;
        }
        // Not all of the processors are in the desired state. Pause for a bit and poll again.
        continuePolling = pause.pause();
    }
    return false;
}
Also used : NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) Revision(org.apache.nifi.web.Revision) ProcessorStatusDTO(org.apache.nifi.web.api.dto.status.ProcessorStatusDTO) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) HashMap(java.util.HashMap) RevisionDTO(org.apache.nifi.web.api.dto.RevisionDTO) Function(java.util.function.Function) AffectedComponentDTO(org.apache.nifi.web.api.dto.AffectedComponentDTO) HttpMethod(javax.ws.rs.HttpMethod) ProcessorEntity(org.apache.nifi.web.api.entity.ProcessorEntity) MediaType(javax.ws.rs.core.MediaType) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) ActivateControllerServicesEntity(org.apache.nifi.web.api.entity.ActivateControllerServicesEntity) Map(java.util.Map) ClusterCoordinator(org.apache.nifi.cluster.coordination.ClusterCoordinator) RequestReplicator(org.apache.nifi.cluster.coordination.http.replication.RequestReplicator) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) NiFiServiceFacade(org.apache.nifi.web.NiFiServiceFacade) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) Logger(org.slf4j.Logger) ControllerServicesEntity(org.apache.nifi.web.api.entity.ControllerServicesEntity) Set(java.util.Set) ProcessorsEntity(org.apache.nifi.web.api.entity.ProcessorsEntity) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Collectors(java.util.stream.Collectors) ReplicationTarget(org.apache.nifi.web.api.ApplicationResource.ReplicationTarget) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ScheduledState(org.apache.nifi.controller.ScheduledState) ControllerServiceState(org.apache.nifi.controller.service.ControllerServiceState) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity) DtoFactory(org.apache.nifi.web.api.dto.DtoFactory) ScheduleComponentsEntity(org.apache.nifi.web.api.entity.ScheduleComponentsEntity) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse) ActivateControllerServicesEntity(org.apache.nifi.web.api.entity.ActivateControllerServicesEntity) ControllerServicesEntity(org.apache.nifi.web.api.entity.ControllerServicesEntity) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ControllerServiceEntity(org.apache.nifi.web.api.entity.ControllerServiceEntity) AffectedComponentEntity(org.apache.nifi.web.api.entity.AffectedComponentEntity)

Example 92 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class AbstractClient method setAllHeaders.

protected static void setAllHeaders(MultivaluedMap<String, String> headers, HttpURLConnection conn) {
    for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < entry.getValue().size(); i++) {
            String value = entry.getValue().get(i);
            b.append(value);
            if (i + 1 < entry.getValue().size()) {
                b.append(',');
            }
        }
        conn.setRequestProperty(entry.getKey(), b.toString());
    }
}
Also used : MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ArrayList(java.util.ArrayList) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 93 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class InjectionUtils method injectIntoMap.

// CHECKSTYLE:OFF
private static Object injectIntoMap(Class<?> rawType, Type genericType, Annotation[] paramAnns, MultivaluedMap<String, String> processedValues, boolean decoded, ParameterType pathParam, Message message) {
    // CHECKSTYLE:ON
    ParameterizedType paramType = (ParameterizedType) genericType;
    Class<?> keyType = (Class<?>) paramType.getActualTypeArguments()[0];
    Type secondType = InjectionUtils.getType(paramType.getActualTypeArguments(), 1);
    if (secondType instanceof ParameterizedType) {
        MultivaluedMap<Object, Object> theValues = new MetadataMap<Object, Object>();
        ParameterizedType valueParamType = (ParameterizedType) secondType;
        Class<?> valueType = (Class<?>) InjectionUtils.getType(valueParamType.getActualTypeArguments(), 0);
        for (Map.Entry<String, List<String>> processedValuesEntry : processedValues.entrySet()) {
            List<String> valuesList = processedValuesEntry.getValue();
            for (String value : valuesList) {
                Object o = InjectionUtils.handleParameter(value, decoded, valueType, valueType, paramAnns, pathParam, message);
                theValues.add(convertStringToPrimitive(processedValuesEntry.getKey(), keyType), o);
            }
        }
        return theValues;
    }
    Map<Object, Object> theValues = new HashMap<>();
    Class<?> valueType = (Class<?>) InjectionUtils.getType(paramType.getActualTypeArguments(), 1);
    for (Map.Entry<String, List<String>> processedValuesEntry : processedValues.entrySet()) {
        List<String> valuesList = processedValuesEntry.getValue();
        for (String value : valuesList) {
            Object o = InjectionUtils.handleParameter(value, decoded, valueType, valueType, paramAnns, pathParam, message);
            theValues.put(convertStringToPrimitive(processedValuesEntry.getKey(), keyType), o);
        }
    }
    return theValues;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterType(org.apache.cxf.jaxrs.model.ParameterType) MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) List(java.util.List) ArrayList(java.util.ArrayList) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 94 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project ORCID-Source by ORCID.

the class OrcidJacksonJaxbJsonProviderPretty method readFrom.

/**
 * This adds a validation step when converting JSON into ORCID models.
 */
@Override
public Object readFrom(Class<Object> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5) throws IOException {
    Object o = null;
    try {
        o = super.readFrom(arg0, arg1, arg2, arg3, arg4, arg5);
    } catch (JsonMappingException e) {
        Map<String, String> params = new HashMap<>();
        params.put("error", e.getMessage());
        throw new InvalidJSONException(params);
    }
    if (jsonInputValidator.canValidate(o.getClass())) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        String apiVersion = (String) requestAttributes.getAttribute(ApiVersionFilter.API_VERSION_REQUEST_ATTRIBUTE_NAME, RequestAttributes.SCOPE_REQUEST);
        if (apiVersion != null && apiVersion.equals("2.1")) {
            jsonInputValidator.validate2_1APIJSONInput(o);
        } else {
            jsonInputValidator.validateJSONInput(o);
        }
    }
    return o;
}
Also used : InvalidJSONException(org.orcid.core.exception.InvalidJSONException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RequestAttributes(org.springframework.web.context.request.RequestAttributes) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 95 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project com-liferay-apio-architect by liferay.

the class FailureFilter method filter.

@Override
public void filter(ContainerRequestContext containerRequestContext, ContainerResponseContext containerResponseContext) throws IOException {
    Try<Object> objectTry = Try.fromFallible(containerResponseContext::getEntity);
    objectTry.map(Failure.class::cast).map(Failure::getException).map(exception -> _errorUtil.getErrorResponse(exception, _request, _httpHeaders)).ifSuccess(response -> {
        containerResponseContext.setStatus(response.getStatus());
        MultivaluedMap<String, Object> headers = containerResponseContext.getHeaders();
        headers.remove(CONTENT_TYPE);
        MediaType mediaType = response.getMediaType();
        if (mediaType != null) {
            headers.add(CONTENT_TYPE, mediaType.toString());
        }
        Object entity = response.getEntity();
        if (entity != null) {
            containerResponseContext.setEntity(entity);
        }
    });
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) Try(com.liferay.apio.architect.functional.Try) Context(javax.ws.rs.core.Context) IOException(java.io.IOException) Failure(com.liferay.apio.architect.functional.Try.Failure) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) ErrorUtil(com.liferay.apio.architect.jaxrs.json.internal.util.ErrorUtil) MediaType(javax.ws.rs.core.MediaType) Component(org.osgi.service.component.annotations.Component) HttpHeaders(javax.ws.rs.core.HttpHeaders) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) MediaType(javax.ws.rs.core.MediaType) Failure(com.liferay.apio.architect.functional.Try.Failure)

Aggregations

MultivaluedMap (javax.ws.rs.core.MultivaluedMap)135 Map (java.util.Map)67 List (java.util.List)51 HashMap (java.util.HashMap)45 MediaType (javax.ws.rs.core.MediaType)35 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)28 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)27 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)23 Test (org.junit.Test)21 WebApplicationException (javax.ws.rs.WebApplicationException)19 LinkedHashMap (java.util.LinkedHashMap)18 Type (java.lang.reflect.Type)16 Response (javax.ws.rs.core.Response)16 InputStream (java.io.InputStream)14 OutputStream (java.io.OutputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)13 Method (java.lang.reflect.Method)11 Optional (java.util.Optional)11