Skip to content

Instantly share code, notes, and snippets.

@guilhermesilveira
Created October 14, 2010 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermesilveira/626585 to your computer and use it in GitHub Desktop.
Save guilhermesilveira/626585 to your computer and use it in GitHub Desktop.
@Resource
public class SoftwareResource {
@Post @Consumes
  public void install(Software software) {
    software = SoftwareRepository.register(software);
    response.created(software);
  }
}
Link link = resource(user).getLink("machine");
response = link.follow().post(new Machine());
double amount = resource(user).refresh().
getAmountDue();
public interface Queue {
}
@ApplicationScoped
public class DefaultQueue {
}
@Resource
public class Posts {
public Posts(Queue q) {
// do as you wish
}
}
@Resource
public class MachineResource {
@Post @Path("{m.id}/softwares")
public SoftwareResource softwares(Machine m) {
Machine machine = new MachineRepository().retrieve(m);
// ...
}
}
@RequestScoped
@Lazy // optionally marking this interceptor as lazy loaded
@Intercepts
public class CustomInterceptor implements Interceptor {
public boolean accepts(ResourceMethod method) {
return true; // if you want it to intercept this resource
}
public void intercept(InterceptorStack stack, ResourceMethod method, Object instance) throws InterceptionException {
// access to any injected components, the stack or the resource itself
stack.next(method, instance);
}
}
@Resource
public class SoftwareController {
public Software show(long id) {
Software retrieved = dao.search(id);
return retrieved;
}
}
@Resource
public class UserController {
// inject dao
public User show(long id) {
return dao.load(id);
}
}
@Resource
public class AdminController {
private final Result result;
private final UserDao dao;
// easy to mock, inject => easy to test
public AdminController(Result result, UserDao dao) {
this.result = result;
this.dao = dao;
}
public void create(User created) {
User created = dao.save(prototype);
// refactor friendly + no URI hell
result.redirectTo(UserController.class).show(created);
}
}
public File download(Document d) {
return manager.load(d).getDocumentPath();
}
public InputStream download(Music m) {
return manager.load(m).getStream();
}
public void upload(File music) {
manager.save(new Music(music));
// redirect somewhere else
}
@Resource
public class SoftwareResource {
@Post @Consumes
  public void install(Software software) {
    // ...
    result.use(SoftwareResource.class).show(software);
  }
}
  @GET @Path("/softwares/{id}")
  public Response install(@QueryParam("id") Software software) {
// ...
  }
validator.checking(new Validations(){{
that(employee.getRoles(), hasItem("ADMIN"));
}});
validator.onErrorUsePageOf(LoginController.class).login();
dao.add(employee);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment