com.google.api.server.spi.config.Named Java Examples

The following examples show how to use com.google.api.server.spi.config.Named. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApiConfigAnnotationReaderTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializedParameter() throws Exception {
  @Api
  final class Test {
    @SuppressWarnings("unused")
    public void method(@Named("serialized") TestBean tb) {}
  }

  ApiConfig config = createConfig(Test.class);
  annotationReader.loadEndpointClass(serviceContext, Test.class, config);
  annotationReader.loadEndpointMethods(serviceContext, Test.class,
      config.getApiClassConfig().getMethods());

  ApiMethodConfig methodConfig =
      config.getApiClassConfig().getMethods().get(methodToEndpointMethod(Test.class.getMethod(
          "method", TestBean.class)));
  validateMethod(methodConfig, "Test.method", "method/{serialized}", ApiMethod.HttpMethod.POST,
      DEFAULT_SCOPES,
      DEFAULT_AUDIENCES,
      DEFAULT_CLIENTIDS,
      null,
      null);
  validateParameter(methodConfig.getParameterConfigs().get(0), "serialized", false, null,
      TestBean.class, TestSerializer.class, String.class);
}
 
Example #2
Source File: AdSearchEndpoints.java    From AdSearch_Endpoints with Apache License 2.0 6 votes vote down vote up
@ApiMethod(name = "getTokens", path = "getTokens",
        httpMethod = HttpMethod.GET)
public List<String> getTokens(@Named("name") String queryString) {

	// add Ads data
	AdsData ads1 = new AdsData((long) 1231, (long) 66, "basketball kobe shoe nike", 0.37f, 6.0f);
	ofy().save().entity(ads1).now();
	AdsData ads2 = new AdsData((long) 1232, (long) 66, "soccer shoe nike", 0.23f, 4.0f);
	ofy().save().entity(ads2).now();
	AdsData ads3 = new AdsData((long) 1233, (long) 67, "running shoe adidas", 0.53f, 7.5f);
	ofy().save().entity(ads3).now();
	AdsData ads4 = new AdsData((long) 1234, (long) 67, "soccer shoe adidas", 0.19f, 3.5f);
	ofy().save().entity(ads4).now();
	AdsData ads5 = new AdsData((long) 1235, (long) 67, "basketball shoe adidas", 0.29f, 5.5f);
	ofy().save().entity(ads5).now();
	
	// add Campaign data
	CampaignData cmp1 = new CampaignData((long) 66, 1500f);
	ofy().save().entity(cmp1).now();    	
	CampaignData cmp2 = new CampaignData((long) 67, 2800f);
	ofy().save().entity(cmp2).now();       	
	CampaignData cmp3 = new CampaignData((long) 68, 900f);
	ofy().save().entity(cmp3).now();   
	
    return QUERY_PARSER.parseQuery(queryString);
}
 
Example #3
Source File: ApiConfigAnnotationReaderTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testParameterAnnotations() throws Exception {
  @Api
  class Endpoint {
    @SuppressWarnings("unused")
    public void method(@Named("foo") @Nullable @DefaultValue("4") int foo) {}
  }

  ApiConfig config = createConfig(Endpoint.class);
  annotationReader.loadEndpointClass(serviceContext, Endpoint.class, config);
  annotationReader.loadEndpointMethods(serviceContext, Endpoint.class,
      config.getApiClassConfig().getMethods());

  ApiMethodConfig methodConfig =
      Iterables.getOnlyElement(config.getApiClassConfig().getMethods().values());
  ApiParameterConfig parameterConfig =
      Iterables.getOnlyElement(methodConfig.getParameterConfigs());
  validateParameter(parameterConfig, "foo", true, "4", int.class, null, int.class);
}
 
Example #4
Source File: TestEndpoint.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@ApiMethod(httpMethod = HttpMethod.GET, path = "testparamsquery")
public FieldContainer testParamsQuery(
    @Named("anint") Integer anInt,
    @Named("along") Long aLong,
    @Named("afloat") Float aFloat,
    @Named("adouble") Double aDouble,
    @Named("aboolean") Boolean aBoolean,
    @Named("astring") String aString,
    @Named("asimpledate") SimpleDate aSimpleDate,
    @Named("adateandtime") DateAndTime aDateAndTime,
    @Named("adate") Date aDate,
    @Named("anenum") TestEnum anEnum) {
  FieldContainer ret = new FieldContainer();
  ret.anInt = anInt;
  ret.aLong = aLong;
  ret.aFloat = aFloat;
  ret.aDouble = aDouble;
  ret.aBoolean = aBoolean;
  ret.aString = aString;
  ret.aSimpleDate = aSimpleDate;
  ret.aDateAndTime = aDateAndTime;
  ret.aDate = aDate;
  ret.anEnum = anEnum;
  return ret;
}
 
Example #5
Source File: TestEndpoint.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@ApiMethod(httpMethod = HttpMethod.GET, path = "testparamspath/{anint}/{along}/{afloat}/{adouble}"
    + "/{aboolean}/{astring}/{asimpledate}/{adateandtime}/{adate}/{anenum}")
public FieldContainer testParamsPath(
    @Named("anint") int anInt,
    @Named("along") long aLong,
    @Named("afloat") float aFloat,
    @Named("adouble") double aDouble,
    @Named("aboolean") boolean aBoolean,
    @Named("astring") String aString,
    @Named("asimpledate") SimpleDate aSimpleDate,
    @Named("adateandtime") DateAndTime aDateAndTime,
    @Named("adate") Date aDate,
    @Named("anenum") TestEnum anEnum) {
  FieldContainer ret = new FieldContainer();
  ret.anInt = anInt;
  ret.aLong = aLong;
  ret.aFloat = aFloat;
  ret.aDouble = aDouble;
  ret.aBoolean = aBoolean;
  ret.aString = aString;
  ret.aSimpleDate = aSimpleDate;
  ret.aDateAndTime = aDateAndTime;
  ret.aDate = aDate;
  ret.anEnum = anEnum;
  return ret;
}
 
Example #6
Source File: ApiConfigAnnotationReaderTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testParameterAnnotations_javax() throws Exception {
  @Api
  class Endpoint {
    @SuppressWarnings("unused")
    public void method(@javax.inject.Named("foo") @javax.annotation.Nullable int foo) {}
  }

  ApiConfig config = createConfig(Endpoint.class);
  annotationReader.loadEndpointClass(serviceContext, Endpoint.class, config);
  annotationReader.loadEndpointMethods(serviceContext, Endpoint.class,
      config.getApiClassConfig().getMethods());

  ApiMethodConfig methodConfig =
      Iterables.getOnlyElement(config.getApiClassConfig().getMethods().values());
  ApiParameterConfig parameterConfig =
      Iterables.getOnlyElement(methodConfig.getParameterConfigs());
  validateParameter(parameterConfig, "foo", true, null, int.class, null, int.class);
}
 
Example #7
Source File: ServletRequestParamReader.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
protected static List<String> getParameterNames(EndpointMethod endpointMethod)
    throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  List<String> parameterNames = endpointMethod.getParameterNames();
  if (parameterNames == null) {
    Method method = endpointMethod.getMethod();
    parameterNames = new ArrayList<>();
    for (int parameter = 0; parameter < method.getParameterTypes().length; parameter++) {
      Annotation annotation = AnnotationUtil.getNamedParameter(method, parameter, Named.class);
      if (annotation == null) {
        parameterNames.add(null);
      } else {
        parameterNames.add((String) annotation.getClass().getMethod("value").invoke(annotation));
      }
    }
    endpointMethod.setParameterNames(parameterNames);
  }
  return parameterNames;
}
 
Example #8
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testJavaxNamed() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void foo(
        @javax.inject.Named("str") String str,
        @Nullable @javax.inject.Named("i") Integer i) {}
  }
  String requestString = "{\"str\":\"hello\"}";

  Method method = Test.class.getDeclaredMethod("foo", String.class, Integer.class);
  Object[] params = readParameters(requestString, method);

  assertEquals(2, params.length);
  assertEquals("hello", params[0]);
  assertNull(params[1]);
}
 
Example #9
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testCachedNamesAreUsed() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void foo(@Named("foo1") String f1, @Nullable @Named("foo2") String f2,
        @Named("foo3") String f3) {}
  }

  Method method = Test.class.getDeclaredMethod("foo", String.class, String.class, String.class);
  EndpointMethod endpointMethod = EndpointMethod.create(method.getDeclaringClass(), method);
  endpointMethod.setParameterNames(ImmutableList.of("name1", "name2", "name3"));

  String requestString = "{\"name1\":\"v1\", \"foo2\":\"v2\", \"name3\":\"v3\"}";

  Object[] params = readParameters(requestString, endpointMethod);

  assertEquals(3, params.length);
  assertEquals("v1", params[0]);
  assertNull(params[1]);
  assertEquals("v3", params[2]);
}
 
Example #10
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testNamesAreCached() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void foo(
        @Nullable @Named("foo1") String f1,
        @Nullable @Named("foo2") String f2,
        @Nullable @Named("foo3") String f3) {}
  }

  Method method = Test.class.getDeclaredMethod("foo", String.class, String.class, String.class);
  EndpointMethod endpointMethod = EndpointMethod.create(method.getDeclaringClass(), method);

  readParameters("{}", endpointMethod);
  List<String> parameterNames = endpointMethod.getParameterNames();

  assertEquals(3, parameterNames.size());
  assertEquals("foo1", parameterNames.get(0));
  assertEquals("foo2", parameterNames.get(1));
  assertEquals("foo3", parameterNames.get(2));
}
 
Example #11
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadGenericDateCollectionParameters() throws Exception {
  class TestGeneric<T> {
    @SuppressWarnings("unused")
    public void collection(@Named("collection") Collection<T> dates) {}
  }
  class Test extends TestGeneric<Date> {}
  doTestReadCollectionDateParameter("collection", EndpointMethod.create(
      Test.class, Test.class.getMethod("collection", Collection.class),
      TypeToken.of(Test.class).getSupertype(TestGeneric.class)));
}
 
Example #12
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadSetParameters() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void collection(@Named("set") Set<Integer> integers) {}
  }
  Method method = Test.class.getDeclaredMethod("collection", Set.class);
  doTestSetParameter(
      "set", EndpointMethod.create(Test.class, method));
}
 
Example #13
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadListParameters() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void collection(@Named("list") List<Integer> integers) {}
  }
  Method method = Test.class.getDeclaredMethod("collection", List.class);
  doTestCollectionParameter(
      "list", EndpointMethod.create(Test.class, method));
}
 
Example #14
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadArrayParameters() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void array(@Named("array") Integer[] integers) {}
  }
  Method method = Test.class.getDeclaredMethod("array", Integer[].class);
  doTestReadArrayParameter(
      "array", EndpointMethod.create(Test.class, method));
}
 
Example #15
Source File: Endpoint1.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@ApiMethod(
    name = "foos.execute0",
    path = "execute0",
    httpMethod = "POST"
)
public Object execute0(@Named("id") String id, @Named("i0") int i0,
    @Named("i1") @Nullable Integer i1, @Named("long0") long long0,
    @Nullable @Named("long1") Long long1, @Named("b0") boolean b0,
    @Nullable @Named("b1") Boolean b1, @Named("f") float f,
    @Nullable @Named("d") Double d) {
  return null;
}
 
Example #16
Source File: Endpoint1.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@ApiMethod(
    name = "foos.remove",
    path = "foos/{id}",
    httpMethod = HttpMethod.DELETE,
    cacheControl = @ApiMethodCacheControl(
        noCache = false,
        maxAge = 5
    )
)
public void removeFoo(@Named("id") String id) {
}
 
Example #17
Source File: Endpoint1.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@ApiMethod(
    name = "foos.update",
    path = "foos/{id}",
    httpMethod = HttpMethod.PUT,
    cacheControl = @ApiMethodCacheControl(
        noCache = false,
        maxAge = 4
    )
)
public Foo updateFoo(@Named("id") String id, Foo r) {
  return null;
}
 
Example #18
Source File: RegistrationEndpoint.java    From MobileShoppingAssistant-sample with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a collection of registered devices.
 * @param count The number of devices to list
 * @param user the user listing registered devices.*
 * @return a list of Google Cloud Messaging registration Ids
 * @throws com.google.api.server.spi.response.UnauthorizedException if
 * user is unauthorized
 */
@ApiMethod(httpMethod = "GET")
public final CollectionResponse<Registration> listDevices(
        @Named("count") final int count,
        final User user) throws UnauthorizedException {
    EndpointUtil.throwIfNotAdmin(user);
    List<Registration> records = ofy().load()
            .type(Registration.class).limit(count)
            .list();
    return CollectionResponse.<Registration>builder()
            .setItems(records).build();
}
 
Example #19
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadDateCollectionParameters() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void collection(@Named("collection") Collection<Date> dates) {}
  }
  Method method = Test.class.getDeclaredMethod("collection", Collection.class);
  doTestReadCollectionDateParameter(
      "collection", EndpointMethod.create(Test.class, method));
}
 
Example #20
Source File: ApiConfigAnnotationReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidDefaultValuedParameterInteger() throws Exception {
  final class Test extends DefaultValuedEndpoint<Integer> {
    @Override public void foo(@Named("id") @DefaultValue("2718") Integer id) {}
  }
  assertEquals(2718, Integer.parseInt(implValidTestDefaultValuedParameter(Test.class)));
}
 
Example #21
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadDateArrayParameters() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void array(@Named("array") Date[] dates) {}
  }
  Method method = Test.class.getDeclaredMethod("array", Date[].class);
  doTestReadArrayDateParameter(
      "array", EndpointMethod.create(Test.class, method));
}
 
Example #22
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadDateGenericArrayParameters() throws Exception {
  class TestGeneric<T> {
    @SuppressWarnings("unused")
    public void array(@Named("array") T[] dates) {}
  }
  class Test extends TestGeneric<Date> {}
  doTestReadArrayDateParameter("array", EndpointMethod.create(
      Test.class, Test.class.getMethod("array", Object[].class),
      TypeToken.of(Test.class).getSupertype(TestGeneric.class)));
}
 
Example #23
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadEnumCollectionParameters() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void collection(@Named("collection") Collection<Outcome> outcomes) {}
  }
  Method method = Test.class.getDeclaredMethod("collection", Collection.class);
  doTestReadCollectionEnumParameter(
      "collection", EndpointMethod.create(Test.class, method));
}
 
Example #24
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadGenericEnumCollectionParameters() throws Exception {
  class TestGeneric<T> {
    @SuppressWarnings("unused")
    public void collection(@Named("collection") Collection<T> outcomes) {}
  }
  class Test extends TestGeneric<Outcome> {}
  doTestReadCollectionEnumParameter("collection", EndpointMethod.create(
      Test.class, Test.class.getMethod("collection", Collection.class),
      TypeToken.of(Test.class).getSupertype(TestGeneric.class)));
}
 
Example #25
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadEnumArrayParameters() throws Exception {
  class Test {
    @SuppressWarnings("unused")
    public void array(@Named("array") Outcome[] outcomes) {}
  }
  Method method = Test.class.getDeclaredMethod("array", Outcome[].class);
  doTestReadArrayEnumParameter("array", EndpointMethod.create(Test.class, method));
}
 
Example #26
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadOAuthToken() throws Exception {
  class TestOAuthToken {
    @SuppressWarnings("unused")
    public void oAuthToken(@Named("oauth_token") String token) {}
  }
  Object[] params = readParameters(
      "{\"oauth_token\":\"test\"}",
      TestOAuthToken.class.getDeclaredMethod("oAuthToken", String.class));
  assertEquals(1, params.length);
  assertEquals("test", params[0]);
}
 
Example #27
Source File: ServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testNullValueForRequiredParam() throws Exception {
  class TestNullValueForRequiredParam {
    @SuppressWarnings("unused")
    public void test(@Named("testParam") String testParam) {}
  }
  try {
    Object[] params =
        readParameters("{}",
            TestNullValueForRequiredParam.class.getDeclaredMethod("test", String.class));
    fail("expected bad request exception");
  } catch (BadRequestException ex) {
    // expected
  }
}
 
Example #28
Source File: ApiConfigAnnotationReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidDefaultValuedParameterLong() throws Exception {
  final class Test extends DefaultValuedEndpoint<Long> {
    @Override public void foo(@Named("id") @DefaultValue("3141") Long id) {}
  }
  assertEquals(3141L, Long.parseLong(implValidTestDefaultValuedParameter(Test.class)));
}
 
Example #29
Source File: RestServletRequestParamReaderTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@ApiMethod(
    name = "testFormData",
    httpMethod = HttpMethod.POST,
    path = "testFormData")
public void testFormData(
    @Nullable @Named("foo") String foo,
    @Nullable @Named("bar") Integer bar) {
}
 
Example #30
Source File: UserEndpoint.java    From tech-gallery with Apache License 2.0 5 votes vote down vote up
@ApiMethod(name = "saveUserPreference", path = "users/savePreference", httpMethod = "post")
public TechGalleryUser saveUserPreference(
        @Named("postGooglePlusPreference") Boolean postGooglePlusPreference, User user)
        throws NotFoundException, BadRequestException, InternalServerErrorException, IOException,
        OAuthRequestException {
    return service.saveUserPreference(postGooglePlusPreference, user);
}