public
Last active

  • Download Gist
Conneg.java
Java
1 2 3 4 5 6 7 8 9 10
@Resource
public class SoftwareResource {
 
@Post @Consumes
  public void install(Software software) {
    software = SoftwareRepository.register(software);
    response.created(software);
  }
 
}
Follow.java
Java
1 2 3 4 5 6 7
Link link = resource(user).getLink("machine");
response = link.follow().post(new Machine());
 
 
 
double amount = resource(user).refresh().
getAmountDue();
Injection.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public interface Queue {
}
 
@ApplicationScoped
public class DefaultQueue {
}
 
@Resource
public class Posts {
 
public Posts(Queue q) {
// do as you wish
}
}
InjectionAnd404.java
Java
1 2 3 4 5 6 7 8 9 10
@Resource
public class MachineResource {
 
@Post @Path("{m.id}/softwares")
public SoftwareResource softwares(Machine m) {
Machine machine = new MachineRepository().retrieve(m);
// ...
}
 
}
Interceptor.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
@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);
}
}
Outjection.java
Java
1 2 3 4 5 6 7 8 9
@Resource
public class SoftwareController {
 
public Software show(long id) {
Software retrieved = dao.search(id);
return retrieved;
}
 
}
ResponseBuilder.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
@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);
 
}
 
}
URIDRY.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
@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) {
// ...
  }
UploadDownload.java
Java
1 2 3 4 5 6 7 8 9 10 11 12
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
}
Validator.java
Java
1 2 3 4 5 6 7
validator.checking(new Validations(){{
that(employee.getRoles(), hasItem("ADMIN"));
}});
 
validator.onErrorUsePageOf(LoginController.class).login();
 
dao.add(employee);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.