Example usage for java.util Optional orElse

List of usage examples for java.util Optional orElse

Introduction

In this page you can find the example usage for java.util Optional orElse.

Prototype

public T orElse(T other) 

Source Link

Document

If a value is present, returns the value, otherwise returns other .

Usage

From source file:com.orange.ngsi2.server.Ngsi2BaseController.java

/**
 * Endpoint put /v2/entities/{entityId}/attrs/{attrName}/value
 * @param entityId the entity ID// w w  w .  java  2  s .  c o  m
 * @param attrName the attribute name
 * @param type an optional type of entity
 * @return http status 204 (No Content) or 409 (conflict)
 * @throws Exception
 */
@RequestMapping(method = RequestMethod.PUT, value = {
        "/entities/{entityId}/attrs/{attrName}/value" }, consumes = MediaType.TEXT_PLAIN_VALUE)
final public ResponseEntity updatePlainTextAttributeValueEndpoint(@PathVariable String entityId,
        @PathVariable String attrName, @RequestParam Optional<String> type, @RequestBody String value)
        throws Exception {

    validateSyntax(entityId, type.orElse(null), attrName);
    updateAttributeValue(entityId, attrName, type.orElse(null), Ngsi2ParsingHelper.parseTextValue(value));
    return new ResponseEntity(HttpStatus.NO_CONTENT);
}

From source file:com.orange.ngsi2.server.Ngsi2BaseController.java

/**
 * Endpoint put /v2/entities/{entityId}/attrs/{attrName}
 * @param entityId the entity ID/* w w  w  . j  av  a2  s .co  m*/
 * @param attrName the attribute name
 * @param type an optional type of entity
 * @return http status 204 (no content) or 409 (conflict)
 * @throws Exception
 */
@RequestMapping(method = RequestMethod.PUT, value = {
        "/entities/{entityId}/attrs/{attrName}" }, consumes = MediaType.APPLICATION_JSON_VALUE)
final public ResponseEntity updateAttributeByEntityIdEndpoint(@PathVariable String entityId,
        @PathVariable String attrName, @RequestParam Optional<String> type, @RequestBody Attribute attribute)
        throws Exception {

    validateSyntax(entityId, type.orElse(null), attrName);
    validateSyntax(attribute);
    updateAttributeByEntityId(entityId, attrName, type.orElse(null), attribute);
    return new ResponseEntity(HttpStatus.NO_CONTENT);
}

From source file:com.orange.ngsi2.server.Ngsi2BaseController.java

/**
 * Endpoint patch /v2/entities/{entityId}
 * @param entityId the entity ID//from  ww  w .j  a v  a 2 s. c  o m
 * @param attributes the attributes to update
 * @param type an optional type of entity
 * @param options keyValues is not supported.
 * @return http status 204 (no content)
 * @throws Exception
 */
@RequestMapping(method = RequestMethod.PATCH, value = {
        "/entities/{entityId}" }, consumes = MediaType.APPLICATION_JSON_VALUE)
final public ResponseEntity updateExistingEntityAttributesEndpoint(@PathVariable String entityId,
        @RequestBody HashMap<String, Attribute> attributes, @RequestParam Optional<String> type,
        @RequestParam Optional<String> options) throws Exception {

    validateSyntax(entityId, type.orElse(null), attributes);
    //TODO: to support keyValues as options
    if (options.isPresent()) {
        throw new UnsupportedOptionException(options.get());
    }
    updateExistingEntityAttributes(entityId, type.orElse(null), attributes);
    return new ResponseEntity(HttpStatus.NO_CONTENT);
}

From source file:com.orange.ngsi2.server.Ngsi2BaseController.java

/**
 * Endpoint put /v2/entities/{entityId}//from   w w  w .j  av  a 2 s  .com
 * @param entityId the entity ID
 * @param attributes the new set of attributes
 * @param type an optional type of entity
 * @param options keyValues is not supported.
 * @return http status 204 (no content)
 * @throws Exception
 */
@RequestMapping(method = RequestMethod.PUT, value = {
        "/entities/{entityId}" }, consumes = MediaType.APPLICATION_JSON_VALUE)
final public ResponseEntity replaceAllEntityAttributesEndpoint(@PathVariable String entityId,
        @RequestBody HashMap<String, Attribute> attributes, @RequestParam Optional<String> type,
        @RequestParam Optional<String> options) throws Exception {

    validateSyntax(entityId, type.orElse(null), attributes);
    //TODO: to support keyValues as options
    if (options.isPresent()) {
        throw new UnsupportedOptionException(options.get());
    }
    replaceAllEntityAttributes(entityId, type.orElse(null), attributes);
    return new ResponseEntity(HttpStatus.NO_CONTENT);
}

From source file:com.ikanow.aleph2.search_service.elasticsearch.services.ElasticsearchIndexService.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//from w  w  w . j  a  v  a 2 s  .com
public <T> Optional<T> getUnderlyingPlatformDriver(final Class<T> driver_class,
        final Optional<String> driver_options) {
    if (Client.class.isAssignableFrom(driver_class)) {
        return (Optional<T>) Optional.of(_crud_factory.getClient());
    }
    if (IAnalyticsAccessContext.class.isAssignableFrom(driver_class)) {
        final String[] owner_bucket_config = driver_options.orElse("unknown:/unknown:{}").split(":", 3);

        if (InputFormat.class.isAssignableFrom(
                AnalyticsUtils.getTypeName((Class<? extends IAnalyticsAccessContext>) driver_class))) { // INPUT FORMAT
            return (Optional<T>) driver_options.filter(__ -> 3 == owner_bucket_config.length)
                    .map(__ -> BeanTemplateUtils.from(owner_bucket_config[2],
                            AnalyticThreadJobBean.AnalyticThreadJobInputBean.class))
                    .map(job_input -> ElasticsearchHadoopUtils.getInputFormat(_crud_factory.getClient(),
                            job_input.get()))
                    .map(access_context -> AnalyticsUtils.injectImplementation(
                            (Class<? extends IAnalyticsAccessContext>) driver_class, access_context));
        } else if (DataFrame.class.isAssignableFrom(
                AnalyticsUtils.getTypeName((Class<? extends IAnalyticsAccessContext>) driver_class))) { // SCHEMA RDD
            return (Optional<T>) driver_options.filter(__ -> 3 == owner_bucket_config.length)
                    .map(__ -> BeanTemplateUtils.from(owner_bucket_config[2],
                            AnalyticThreadJobBean.AnalyticThreadJobInputBean.class))
                    .map(job_input -> ElasticsearchSparkUtils.getDataFrame(_crud_factory.getClient(),
                            job_input.get()))
                    .map(access_context -> AnalyticsUtils.injectImplementation(
                            (Class<? extends IAnalyticsAccessContext>) driver_class, access_context));
        }
    }
    return Optional.empty();
}

From source file:com.orange.ngsi2.server.Ngsi2BaseController.java

/**
 * Endpoint post /v2/entities/{entityId}
 * @param entityId the entity ID/*w  w w.jav  a  2 s.  com*/
 * @param attributes the attributes to update or to append
 * @param type an optional type of entity
 * @param options an optional list of options separated by comma. Possible value for option: append.
 *        keyValues options is not supported.
 *        If append is present then the operation is an append operation
 * @return http status 201 (created)
 * @throws Exception
 */
@RequestMapping(method = RequestMethod.POST, value = {
        "/entities/{entityId}" }, consumes = MediaType.APPLICATION_JSON_VALUE)
final public ResponseEntity updateOrAppendEntityEndpoint(@PathVariable String entityId,
        @RequestBody HashMap<String, Attribute> attributes, @RequestParam Optional<String> type,
        @RequestParam Optional<Set<String>> options) throws Exception {
    validateSyntax(entityId, type.orElse(null), attributes);

    boolean append = false;
    if (options.isPresent()) {
        //TODO: to support keyValues as options
        if (options.get().contains("keyValues")) {
            throw new UnsupportedOptionException("keyValues");
        }
        append = options.get().contains("append");
    }
    updateOrAppendEntity(entityId, type.orElse(null), attributes, append);
    return new ResponseEntity(HttpStatus.NO_CONTENT);
}

From source file:com.orange.ngsi2.server.Ngsi2BaseController.java

/**
 * Endpoint get /v2/entities/{entityId}/*from   w ww. j a  v  a2 s.  com*/
 * @param entityId the entity ID
 * @param type an optional type of entity
 * @param attrs an optional list of attributes to return for the entity
 * @param options an optional list of options separated by comma.
 *        Theses keyValues,values and unique options are not supported.
 * @return the entity and http status 200 (ok) or 409 (conflict)
 * @throws Exception
 */
@RequestMapping(method = RequestMethod.GET, value = { "/entities/{entityId}" })
final public ResponseEntity<Entity> retrieveEntityEndpoint(@PathVariable String entityId,
        @RequestParam Optional<String> type, @RequestParam Optional<List<String>> attrs,
        @RequestParam Optional<String> options) throws Exception {

    validateSyntax(entityId, type.orElse(null), attrs.orElse(null));
    //TODO: to support keyValues, values and unique as options
    if (options.isPresent()) {
        throw new UnsupportedOptionException(options.get());
    }
    return new ResponseEntity<>(retrieveEntity(entityId, type.orElse(null), attrs.orElse(new ArrayList<>())),
            HttpStatus.OK);
}

From source file:com.orange.ngsi2.server.Ngsi2BaseController.java

/**
 * Endpoint get /v2/subscriptions/*from w  w w  . j ava2 s. c  o m*/
 * @param limit an optional limit (0 for none)
 * @param offset an optional offset (0 for none)
 * @param options an optional list of options separated by comma. Possible values for option: count.
 *        If count is present then the total number of entities is returned in the response as a HTTP header named `X-Total-Count`.
 * @return a list of Entities http status 200 (ok)
 * @throws Exception
 */
@RequestMapping(method = RequestMethod.GET, value = { "/subscriptions" })
final public ResponseEntity<List<Subscription>> listSubscriptionsEndpoint(@RequestParam Optional<Integer> limit,
        @RequestParam Optional<Integer> offset, @RequestParam Optional<String> options) throws Exception {

    Paginated<Subscription> paginatedSubscription = listSubscriptions(limit.orElse(0), offset.orElse(0));
    List<Subscription> subscriptionList = paginatedSubscription.getItems();
    if (options.isPresent() && (options.get().contains("count"))) {
        return new ResponseEntity<>(subscriptionList, xTotalCountHeader(paginatedSubscription.getTotal()),
                HttpStatus.OK);
    } else {
        return new ResponseEntity<>(subscriptionList, HttpStatus.OK);
    }
}

From source file:lumbermill.internal.aws.AWSV4SignerImpl.java

public Map<String, String> getSignedHeaders(String uri, String method, Map<String, String> queryParams,
        Map<String, String> headers, Optional<byte[]> payload) {
    final LocalDateTime now = clock.get();
    final AWSCredentials credentials = credentialsProvider.getCredentials();
    final Map<String, String> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    result.putAll(headers);/*from w ww.  jav  a 2 s .com*/
    if (!result.containsKey(DATE)) {
        result.put(X_AMZ_DATE, now.format(BASIC_TIME_FORMAT));
    }
    if (AWSSessionCredentials.class.isAssignableFrom(credentials.getClass())) {
        result.put(SESSION_TOKEN, ((AWSSessionCredentials) credentials).getSessionToken());
    }

    final StringBuilder headersString = new StringBuilder();
    final ImmutableList.Builder<String> signedHeaders = ImmutableList.builder();

    for (Map.Entry<String, String> entry : result.entrySet()) {
        headersString.append(headerAsString(entry)).append(RETURN);
        signedHeaders.add(entry.getKey().toLowerCase());
    }

    final String signedHeaderKeys = JOINER.join(signedHeaders.build());
    final String canonicalRequest = method + RETURN + uri + RETURN + queryParamsString(queryParams) + RETURN
            + headersString.toString() + RETURN + signedHeaderKeys + RETURN
            + toBase16(hash(payload.orElse(EMPTY.getBytes(Charsets.UTF_8))));
    final String stringToSign = createStringToSign(canonicalRequest, now);
    final String signature = sign(stringToSign, now, credentials);
    final String autorizationHeader = AWS4_HMAC_SHA256_CREDENTIAL + credentials.getAWSAccessKeyId() + SLASH
            + getCredentialScope(now) + SIGNED_HEADERS + signedHeaderKeys + SIGNATURE + signature;

    result.put(AUTHORIZATION, autorizationHeader);
    return ImmutableMap.copyOf(result);
}