Created
October 14, 2010 17:12
-
-
Save guilhermesilveira/626585 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Resource | |
public class SoftwareResource { | |
@Post @Consumes | |
public void install(Software software) { | |
software = SoftwareRepository.register(software); | |
response.created(software); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Link link = resource(user).getLink("machine"); | |
response = link.follow().post(new Machine()); | |
double amount = resource(user).refresh(). | |
getAmountDue(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Queue { | |
} | |
@ApplicationScoped | |
public class DefaultQueue { | |
} | |
@Resource | |
public class Posts { | |
public Posts(Queue q) { | |
// do as you wish | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Resource | |
public class MachineResource { | |
@Post @Path("{m.id}/softwares") | |
public SoftwareResource softwares(Machine m) { | |
Machine machine = new MachineRepository().retrieve(m); | |
// ... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Resource | |
public class SoftwareController { | |
public Software show(long id) { | |
Software retrieved = dao.search(id); | |
return retrieved; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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