Java源码示例:com.google.api.server.spi.config.ApiMethod.HttpMethod
示例1
@ApiMethod(httpMethod = ApiMethod.HttpMethod.POST,
path = "images/process/{bucketName}/{objectPath}")
public BlobAccess transformImage(@Named("bucketName") String bucketName,
@Named("objectPath") String objectPath,
@Named("accessMode") BlobAccessMode accessMode,
User user)
throws BadRequestException, UnauthorizedException, InternalServerErrorException, NotFoundException {
validateUser(user);
checkDeletePermissions(bucketName, objectPath, user);
BlobMetadata metadata = BlobManager.getBlobMetadata(bucketName, objectPath);
String transformedObjectPath = String.valueOf("transformed-cloudguestbook-picture-" + System.currentTimeMillis());
BlobAccess blobAccess = getBlobUrlForUpload(bucketName, transformedObjectPath, metadata.getAccessMode(), "");
if (!reserveNameIfAvailable(bucketName, transformedObjectPath, accessMode, user)) {
throw new UnauthorizedException("You don't have permissions to upload the transformed image");
}
// This method is incomplete.
// Implement the rest of the method.
// Complete example is located at MobileBackend/snippets/BlobEndpoints.java
throw new NotFoundException("This method is not implemented yet.");
}
示例2
@ApiMethod(
name = "foos.list",
path = "foos",
httpMethod = HttpMethod.GET,
cacheControl = @ApiMethodCacheControl(
noCache = true,
maxAge = 1
),
scopes = {"s0", "s1 s2"},
audiences = {"a0", "a1"},
clientIds = {"c0", "c1"},
authenticators = { FailAuthenticator.class },
peerAuthenticators = { FailPeerAuthenticator.class }
)
public List<Foo> listFoos() {
return null;
}
示例3
/**
* Deletes a blob.
*
* @param bucketName Google Cloud Storage bucket where the object was uploaded.
* @param objectPath path to the object in the bucket.
* @param user the user making the request.
* @throws com.google.api.server.spi.response.UnauthorizedException if the user is not authorized.
* @throws com.google.api.server.spi.response.BadRequestException if the bucketName or objectPath are not valid.
* @throws com.google.api.server.spi.response.InternalServerErrorException when the operation failed.
*/
@ApiMethod(httpMethod = HttpMethod.DELETE, path = "blobs/{bucketName}/{objectPath}")
public void deleteBlob(
@Named("bucketName") String bucketName, @Named("objectPath") String objectPath, User user)
throws UnauthorizedException, BadRequestException, InternalServerErrorException {
validateUser(user);
validateBucketAndObjectPath(bucketName, objectPath);
boolean blobExists = checkDeletePermissions(bucketName, objectPath, user);
if (!blobExists) {
// DELETE operation is idempotent. The object doesn't exist, so there is no more work to do.
return;
}
if (!deleteAllBlobInformation(bucketName, objectPath)) {
throw new InternalServerErrorException("Deleting blob failed. You can retry.");
}
}
示例4
/**
* Gets a signed URL that can be used to upload a blob.
*
* @param bucketName Google Cloud Storage bucket to use for upload.
* @param objectPath path to the object in the bucket.
* @param accessMode controls how the uploaded blob can be accessed.
* @param contentType the MIME type of the object of be uploaded. Can be null.
* @param user the user making the request.
* @throws com.google.api.server.spi.response.UnauthorizedException if the user is not authorized.
* @throws com.google.api.server.spi.response.BadRequestException if the bucketName or objectPath are not valid.
*/
@ApiMethod(httpMethod = HttpMethod.GET, path = "blobs/uploads/{bucketName}/{objectPath}")
public BlobAccess getUploadUrl(@Named("bucketName") String bucketName,
@Named("objectPath") String objectPath, @Named("accessMode") BlobAccessMode accessMode,
@Nullable @Named("contentType") String contentType, User user)
throws UnauthorizedException, BadRequestException {
validateUser(user);
validateBucketAndObjectPath(bucketName, objectPath);
if (!reserveNameIfAvailable(bucketName, objectPath, accessMode, user)) {
throw new UnauthorizedException("You don't have permissions to upload this object");
}
return getBlobUrlForUpload(
bucketName, objectPath, accessMode, contentType != null ? contentType : "");
}
示例5
/**
* Returns a collection of Conference Object that the user is going to attend.
*
* @param user An user who invokes this method, null when the user is not signed in.
* @return a Collection of Conferences that the user is going to attend.
* @throws UnauthorizedException when the User object is null.
*/
@ApiMethod(
name = "getConferencesToAttend",
path = "getConferencesToAttend",
httpMethod = HttpMethod.GET
)
public Collection<Conference> getConferencesToAttend(final User user)
throws UnauthorizedException, NotFoundException {
// If not signed in, throw a 401 error.
if (user == null) {
throw new UnauthorizedException("Authorization required");
}
Profile profile = ofy().load().key(Key.create(Profile.class, getUserId(user))).now();
if (profile == null) {
throw new NotFoundException("Profile doesn't exist.");
}
List<String> keyStringsToAttend = profile.getConferenceKeysToAttend();
List<Key<Conference>> keysToAttend = new ArrayList<>();
for (String keyString : keyStringsToAttend) {
keysToAttend.add(Key.<Conference>create(keyString));
}
return ofy().load().keys(keysToAttend).values();
}
示例6
/**
* Deletes a blob.
*
* @param bucketName Google Cloud Storage bucket where the object was uploaded.
* @param objectPath path to the object in the bucket.
* @param user the user making the request.
* @throws UnauthorizedException if the user is not authorized.
* @throws BadRequestException if the bucketName or objectPath are not valid.
* @throws InternalServerErrorException when the operation failed.
*/
@ApiMethod(httpMethod = HttpMethod.DELETE, path = "blobs/{bucketName}/{objectPath}")
public void deleteBlob(
@Named("bucketName") String bucketName, @Named("objectPath") String objectPath, User user)
throws UnauthorizedException, BadRequestException, InternalServerErrorException {
validateUser(user);
validateBucketAndObjectPath(bucketName, objectPath);
boolean blobExists = checkDeletePermissions(bucketName, objectPath, user);
if (!blobExists) {
// DELETE operation is idempotent. The object doesn't exist, so there is no more work to do.
return;
}
if (!deleteAllBlobInformation(bucketName, objectPath)) {
throw new InternalServerErrorException("Deleting blob failed. You can retry.");
}
}
示例7
/**
* Creates a new session from {@link WaxNewSessionRequest}.
*
* @return {@link WaxNewSessionResponse} with the created session id
* @throws InternalServerErrorException if the session creation failed
* @throws BadRequestException if the requested session name is bad
*/
@ApiMethod(
name = "sessions.create",
path = "newsession",
httpMethod = HttpMethod.POST)
public WaxNewSessionResponse createSession(WaxNewSessionRequest request)
throws InternalServerErrorException, BadRequestException {
if (Strings.isNullOrEmpty(request.getSessionName())) {
throw new BadRequestException("Name must be non-empty");
}
String sessionId =
store.createSession(request.getSessionName(), request.getDurationInMillis());
if (sessionId != null) {
return new WaxNewSessionResponse(sessionId);
}
throw new InternalServerErrorException("Error while adding session");
}
示例8
/**
* Returns a collection of Conference Object that the user is going to attend.
*
* @param user An user who invokes this method, null when the user is not signed in.
* @return a Collection of Conferences that the user is going to attend.
* @throws UnauthorizedException when the User object is null.
*/
@ApiMethod(
name = "getConferencesToAttend",
path = "getConferencesToAttend",
httpMethod = HttpMethod.GET
)
public Collection<Conference> getConferencesToAttend(final User user)
throws UnauthorizedException, NotFoundException {
// If not signed in, throw a 401 error.
if (user == null) {
throw new UnauthorizedException("Authorization required");
}
Profile profile = ofy().load().key(Key.create(Profile.class, user.getUserId())).now();
if (profile == null) {
throw new NotFoundException("Profile doesn't exist.");
}
List<String> keyStringsToAttend = profile.getConferenceKeysToAttend();
List<Key<Conference>> keysToAttend = new ArrayList<>();
for (String keyString : keyStringsToAttend) {
keysToAttend.add(Key.<Conference>create(keyString));
}
return ofy().load().keys(keysToAttend).values();
}
示例9
@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;
}
示例10
@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);
}
示例11
/**
* Returns a Conference object with the given conferenceId.
*
* @param websafeConferenceKey The String representation of the Conference Key.
* @return a Conference object with the given conferenceId.
* @throws NotFoundException when there is no Conference with the given conferenceId.
*/
@ApiMethod(
name = "getConference",
path = "conference/{websafeConferenceKey}",
httpMethod = HttpMethod.GET
)
public Conference getConference(
@Named("websafeConferenceKey") final String websafeConferenceKey)
throws NotFoundException {
Key<Conference> conferenceKey = Key.create(websafeConferenceKey);
Conference conference = ofy().load().key(conferenceKey).now();
if (conference == null) {
throw new NotFoundException("No Conference found with key: " + websafeConferenceKey);
}
return conference;
}
示例12
/**
* Returns a list of Conferences that the user created.
* In order to receive the websafeConferenceKey via the JSON params, uses a POST method.
*
* @param user A user who invokes this method, null when the user is not signed in.
* @return a list of Conferences that the user created.
* @throws UnauthorizedException when the user is not signed in.
*/
@ApiMethod(
name = "getConferencesCreated",
path = "getConferencesCreated",
httpMethod = HttpMethod.POST
)
public List<Conference> getConferencesCreated(final User user) throws UnauthorizedException {
// If not signed in, throw a 401 error.
if (user == null) {
throw new UnauthorizedException("Authorization required");
}
String userId = user.getUserId();
Key<Profile> userKey = Key.create(Profile.class, userId);
return ofy().load().type(Conference.class)
.ancestor(userKey)
.order("name").list();
}
示例13
/**
* Returns a Conference object with the given conferenceId.
*
* @param websafeConferenceKey The String representation of the Conference Key.
* @return a Conference object with the given conferenceId.
* @throws NotFoundException when there is no Conference with the given conferenceId.
*/
@ApiMethod(
name = "getConference",
path = "conference/{websafeConferenceKey}",
httpMethod = HttpMethod.GET
)
public Conference getConference(
@Named("websafeConferenceKey") final String websafeConferenceKey)
throws NotFoundException {
Key<Conference> conferenceKey = Key.create(websafeConferenceKey);
Conference conference = ofy().load().key(conferenceKey).now();
if (conference == null) {
throw new NotFoundException("No Conference found with key: " + websafeConferenceKey);
}
return conference;
}
示例14
/**
* Returns a collection of Conference Object that the user is going to attend.
*
* @param user An user who invokes this method, null when the user is not signed in.
* @return a Collection of Conferences that the user is going to attend.
* @throws UnauthorizedException when the User object is null.
*/
@ApiMethod(
name = "getConferencesToAttend",
path = "getConferencesToAttend",
httpMethod = HttpMethod.GET
)
public Collection<Conference> getConferencesToAttend(final User user)
throws UnauthorizedException, NotFoundException {
// If not signed in, throw a 401 error.
if (user == null) {
throw new UnauthorizedException("Authorization required");
}
Profile profile = ofy().load().key(Key.create(Profile.class, user.getUserId())).now();
if (profile == null) {
throw new NotFoundException("Profile doesn't exist.");
}
List<String> keyStringsToAttend = profile.getConferenceKeysToAttend();
List<Key<Conference>> keysToAttend = new ArrayList<>();
for (String keyString : keyStringsToAttend) {
keysToAttend.add(Key.<Conference>create(keyString));
}
return ofy().load().keys(keysToAttend).values();
}
示例15
@ApiMethod(
name = "api6.foos.fn2",
path = "fn2",
httpMethod = HttpMethod.GET
)
public Object fn2() {
return null;
}
示例16
@Override
@ApiMethod(
name = "api6.foos.fn1",
path = "fn1",
httpMethod = HttpMethod.GET
)
public Object fn1() {
return null;
}
示例17
@ApiMethod(
name = "foos.get3",
path = "foos/{id}",
httpMethod = HttpMethod.GET,
cacheControl = @ApiMethodCacheControl(
noCache = false,
maxAge = 2
)
)
@Override
public Foo getFoo(@Named("id") String id) {
return null;
}
示例18
/**
* Returns a Profile object associated with the given user object. The cloud
* endpoints system automatically inject the User object.
*
* @param user
* A User object injected by the cloud endpoints.
* @return Profile object.
* @throws UnauthorizedException
* when the User object is null.
*/
@ApiMethod(name = "getProfile", path = "profile", httpMethod = HttpMethod.GET)
public Profile getProfile(final User user) throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException("Authorization required");
}
// TODO
// load the Profile Entity
String userId = user.getUserId();
Key key = Key.create(Profile.class, userId);
Profile profile = (Profile) ofy().load().key(key).now();
return profile;
}
示例19
@ApiMethod(
name = "foos.insert",
path = "foos",
httpMethod = HttpMethod.POST,
cacheControl = @ApiMethodCacheControl(
noCache = false,
maxAge = 3
)
)
public Foo insertFoo(Foo r) {
return null;
}
示例20
@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;
}
示例21
@ApiMethod(
name = "foos.remove",
path = "foos/{id}",
httpMethod = HttpMethod.DELETE,
cacheControl = @ApiMethodCacheControl(
noCache = false,
maxAge = 5
)
)
public void removeFoo(@Named("id") String id) {
}
示例22
@ApiMethod(
name = "getAnnouncement",
path = "announcement",
httpMethod = HttpMethod.GET
)
public Announcement getAnnouncement() {
MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
Object message = memcacheService.get(Constants.MEMCACHE_ANNOUNCEMENTS_KEY);
if (message != null) {
return new Announcement(message.toString());
}
return null;
}
示例23
/** Code to add at the start of querying for conferences **/
@ApiMethod(
name = "queryConferences_nofilters",
path = "queryConferences_nofilters",
httpMethod = HttpMethod.POST
)
public List<Conference> queryConferences_nofilters() {
// Find all entities of type Conference
Query<Conference> query = ofy().load().type(Conference.class).order("name");
return query.list();
}
示例24
/**
* Gets a signed URL that can be used to download a blob.
*
* @param bucketName Google Cloud Storage bucket where the object was uploaded.
* @param objectPath path to the object in the bucket.
* @param user the user making the request.
* @throws com.google.api.server.spi.response.UnauthorizedException if the user is not authorized.
* @throws com.google.api.server.spi.response.BadRequestException if the bucketName or objectPath are not valid.
* @throws com.google.api.server.spi.response.NotFoundException if the object doesn't exist.
*/
@ApiMethod(httpMethod = HttpMethod.GET, path = "blobs/downloads/{bucketName}/{objectPath}")
public BlobAccess getDownloadUrl(
@Named("bucketName") String bucketName, @Named("objectPath") String objectPath, User user)
throws UnauthorizedException, BadRequestException, NotFoundException {
validateUser(user);
validateBucketAndObjectPath(bucketName, objectPath);
checkReadObjectPermissions(bucketName, objectPath, user);
return getBlobUrlForDownload(bucketName, objectPath);
}
示例25
/**
* Remove an existing session.
* <p>
* Clients that create sessions without a duration (will last forever) will need to call this
* method on their own to clean up the session.
*
* @return {@link WaxRemoveSessionResponse} with the deleted session id
* @throws InternalServerErrorException if the session deletion failed
*/
@ApiMethod(
name = "sessions.remove",
path = "removesession",
httpMethod = HttpMethod.POST)
public WaxRemoveSessionResponse removeSession(@Named("sessionId") String sessionId)
throws InternalServerErrorException {
try {
store.deleteSession(sessionId);
return new WaxRemoveSessionResponse(sessionId);
} catch (InvalidSessionException e) {
throw new InternalServerErrorException(e.getMessage());
}
}
示例26
/**
* Returns a Profile object associated with the given user object. The cloud
* endpoints system automatically inject the User object.
*
* @param user
* A User object injected by the cloud endpoints.
* @return Profile object.
* @throws UnauthorizedException
* when the User object is null.
*/
@ApiMethod(name = "getProfile", path = "profile", httpMethod = HttpMethod.GET)
public Profile getProfile(final User user) throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException("Authorization required");
}
// TODO
// load the Profile Entity
String userId = ""; // TODO
Key key = null; // TODO
Profile profile = null; // TODO load the Profile entity
return profile;
}
示例27
/**
* Returns a Profile object associated with the given user object. The cloud
* endpoints system automatically inject the User object.
*
* @param user
* A User object injected by the cloud endpoints.
* @return Profile object.
* @throws UnauthorizedException
* when the User object is null.
*/
@ApiMethod(name = "getProfile", path = "profile", httpMethod = HttpMethod.GET)
public Profile getProfile(final User user) throws UnauthorizedException {
if (user == null) {
throw new UnauthorizedException("Authorization required");
}
// TODO
// load the Profile Entity
String userId = user.getUserId();
Key key = Key.create(Profile.class, userId);
Profile profile = (Profile) ofy().load().key(key).now();
return profile;
}
示例28
@ApiMethod(
name = "queryConferences_nofilters",
path = "queryConferences_nofilters",
httpMethod = HttpMethod.POST
)
public List<Conference> queryConferences_nofilters() {
// Find all entities of type Conference
Query<Conference> query = ofy().load().type(Conference.class).order("name");
return query.list();
}
示例29
@ApiMethod(
name = "getAnnouncement",
path = "announcement",
httpMethod = HttpMethod.GET
)
public Announcement getAnnouncement() {
MemcacheService memcacheService = MemcacheServiceFactory.getMemcacheService();
Object message = memcacheService.get(Constants.MEMCACHE_ANNOUNCEMENTS_KEY);
if (message != null) {
return new Announcement(message.toString());
}
return null;
}
示例30
@ApiMethod(name = "api.foos.base", path = "base", httpMethod = HttpMethod.GET)
public Foo foo(@Named("id") String id) {
return null;
}