package net.eusashead.hateoas.response.argumentresolver;
/*
* #[license]
* spring-responseentitybuilder
* %%
* Copyright (C) 2013 Eusa's Head
* %%
* 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.
* %[license]
*/
import java.util.concurrent.Callable;
import net.eusashead.hateoas.response.DeleteResponseBuilder;
import net.eusashead.hateoas.response.EntityResponseBuilder;
import net.eusashead.hateoas.response.OptionsResponseBuilder;
import net.eusashead.hateoas.response.PostResponseBuilder;
import net.eusashead.hateoas.response.PutResponseBuilder;
import net.eusashead.hateoas.test.model.Entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/async/{name}", consumes="application/json;charset=utf-8")
public class AsyncEntityController {
@Autowired
private EntityService service;
@RequestMapping(method={RequestMethod.GET, RequestMethod.HEAD})
public Callable<ResponseEntity<Entity>> get(@PathVariable final String name, final EntityResponseBuilder<Entity> builder) {
return new Callable<ResponseEntity<Entity>>() {
@Override
public ResponseEntity<Entity> call() throws Exception {
Entity entity = service.getEntity(name);
return builder.entity(entity).etag().build();
}
};
}
@RequestMapping(method=RequestMethod.OPTIONS)
public Callable<ResponseEntity<Entity>> options(final OptionsResponseBuilder<Entity> builder) {
return new Callable<ResponseEntity<Entity>>() {
@Override
public ResponseEntity<Entity> call() throws Exception {
return builder.allow(HttpMethod.GET, HttpMethod.HEAD).entity(new Entity("foo")).build();
}
};
}
@RequestMapping(method=RequestMethod.POST)
public Callable<ResponseEntity<Void>> post(@RequestBody final Entity entity, final PostResponseBuilder builder) {
return new Callable<ResponseEntity<Void>>() {
@Override
public ResponseEntity<Void> call() throws Exception {
Entity created = service.save(entity);
return builder.location("/path/to/{name}", created.getName()).build();
}
};
}
@RequestMapping(method=RequestMethod.PUT)
public Callable<ResponseEntity<Void>> put(@RequestBody final Entity entity, final PutResponseBuilder<Entity> builder) {
return new Callable<ResponseEntity<Void>>() {
@Override
public ResponseEntity<Void> call() throws Exception {
Entity modified = service.save(entity);
return builder.entity(modified).etag().build();
}
};
}
@RequestMapping(method=RequestMethod.DELETE)
public Callable<ResponseEntity<Void>> delete(@PathVariable final String name, final DeleteResponseBuilder builder) {
return new Callable<ResponseEntity<Void>>() {
@Override
public ResponseEntity<Void> call() throws Exception {
service.delete(name);
return builder.build();
}
};
}
}