All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.linkedin.restli.examples.greetings.server.AnnotatedComplexKeysResource Maven / Gradle / Ivy

Go to download

Pegasus is a framework for building robust, scalable service architectures using dynamic discovery and simple asychronous type-checked REST + JSON APIs.

There is a newer version: 27.7.18
Show newest version
/*
   Copyright (c) 2012 LinkedIn Corp.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package com.linkedin.restli.examples.greetings.server;


import com.linkedin.data.transform.DataProcessingException;
import com.linkedin.parseq.promise.Promise;
import com.linkedin.parseq.promise.Promises;
import com.linkedin.parseq.promise.SettablePromise;
import com.linkedin.restli.common.ComplexResourceKey;
import com.linkedin.restli.common.HttpStatus;
import com.linkedin.restli.common.PatchRequest;
import com.linkedin.restli.examples.greetings.api.Message;
import com.linkedin.restli.examples.greetings.api.TwoPartKey;
import com.linkedin.restli.server.BatchDeleteRequest;
import com.linkedin.restli.server.BatchPatchRequest;
import com.linkedin.restli.server.BatchResult;
import com.linkedin.restli.server.BatchUpdateRequest;
import com.linkedin.restli.server.BatchUpdateResult;
import com.linkedin.restli.server.CreateResponse;
import com.linkedin.restli.server.UpdateResponse;
import com.linkedin.restli.server.annotations.Finder;
import com.linkedin.restli.server.annotations.QueryParam;
import com.linkedin.restli.server.annotations.RestLiCollection;
import com.linkedin.restli.server.annotations.RestMethod;
import com.linkedin.restli.server.annotations.TestMethod;
import com.linkedin.restli.server.resources.KeyValueResource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


@RestLiCollection(name="annotatedComplexKeys", namespace = "com.linkedin.restli.examples.greetings.client", keyName="annotatedComplexKeyId")
public class AnnotatedComplexKeysResource
    implements KeyValueResource, Message>
{
  private static ComplexKeysDataProvider _dataProvider = new ComplexKeysDataProvider();
  private static final ScheduledExecutorService _scheduler = Executors.newScheduledThreadPool(1);

  private static final int DELAY = 100;

  @RestMethod.Get
  public Promise get(final ComplexResourceKey key)
  {
    final SettablePromise result = Promises.settable();
    final Runnable requestHandler = new Runnable() {
      public void run()
      {
        result.done(_dataProvider.get(key));
      }
    };

    _scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);

    return result;
  }

  @RestMethod.Create
  public Promise create(final Message message)
  {
    final SettablePromise result = Promises.settable();
    final Runnable requestHandler = new Runnable() {
      public void run()
      {
        ComplexResourceKey key = _dataProvider.create(message);
        result.done(new CreateResponse(key));
      }
    };

    _scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);

    return result;
  }

  @RestMethod.PartialUpdate
  public Promise update(final ComplexResourceKey key,
                               final PatchRequest patch)
  {
    final SettablePromise result = Promises.settable();
    final Runnable requestHandler = new Runnable() {
      public void run()
      {
        try
        {
          _dataProvider.partialUpdate(key, patch);
          result.done(new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
        }
        catch (DataProcessingException e)
        {
          result.done(new UpdateResponse(HttpStatus.S_400_BAD_REQUEST));
        }
      }
    };

    _scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);

    return result;
  }

  /**
   * Example javadoc
   */
  @TestMethod(doc = "For integration tests only.")
  @Finder("prefix")
  public Promise> prefix(@QueryParam("prefix")final String prefix)
  {
    final SettablePromise> result = Promises.settable();
    final Runnable requestHandler = new Runnable() {
      public void run()
      {
        result.done(_dataProvider.findByPrefix(prefix));
      }
    };

    _scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);

    return result;
  }

  @RestMethod.BatchGet
  public Promise, Message>> batchGet(
      final Set> ids)
  {
    final SettablePromise, Message>> result = Promises.settable();
    final Runnable requestHandler = new Runnable() {
      public void run()
      {
        result.done(_dataProvider.batchGet(ids));
      }
    };

    _scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);

    return result;
  }

  @RestMethod.BatchUpdate
  public BatchUpdateResult, Message> batchUpdate(
      final BatchUpdateRequest, Message> entities)
  {
    return _dataProvider.batchUpdate(entities);
  }

  @RestMethod.BatchPartialUpdate
  public BatchUpdateResult, Message> batchUpdate(
      final BatchPatchRequest, Message> patches)
  {
    return _dataProvider.batchUpdate(patches);
  }

  @RestMethod.BatchDelete
  public BatchUpdateResult, Message> batchDelete(
      final BatchDeleteRequest, Message> ids)
  {
    return _dataProvider.batchDelete(ids);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy