br.com.caelum.vraptor.view.Results Java Examples

The following examples show how to use br.com.caelum.vraptor.view.Results. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HistoryController.java    From mamute with Apache License 2.0 6 votes vote down vote up
@SimpleBrutauthRules({EnvironmentKarmaRule.class})
@EnvironmentAccessLevel(PermissionRules.MODERATE_EDITS)
   @Post
   public void publish(Long moderatableId, String moderatableType, Long aprovedInformationId,  String aprovedInformationType) {
   	Class<?> moderatableClass = urlMapping.getClassFor(moderatableType);
   	Information approved = informations.getById(aprovedInformationId, aprovedInformationType);
   	
       Moderatable moderatable = moderatables.getById(moderatableId, moderatableClass);
       List<Information> pending = informations.pendingFor(moderatableId, moderatableClass);
       
       if (!approved.isPending()) {
       	result.use(Results.http()).sendError(403);
       	return;
       }
       
       User approvedAuthor = approved.getAuthor();
       refusePending(aprovedInformationId, pending);
       currentUser.getCurrent().approve(moderatable, approved, environmentKarma);
       ReputationEvent editAppoved = new ReputationEvent(EventType.EDIT_APPROVED, moderatable.getQuestion(), approvedAuthor);
       int karma = calculator.karmaFor(editAppoved);
       approvedAuthor.increaseKarma(karma);
       reputationEvents.save(editAppoved);
       
       result.redirectTo(this).history();
   }
 
Example #2
Source File: CommentController.java    From mamute with Apache License 2.0 6 votes vote down vote up
@Delete
public void delete(Long commentId, String onWhat, Long postId) {
	Comment comment = comments.getById(commentId);
	if (!environment.supports("deletable.comments") || comment == null) {
		result.notFound();
		return;
	}
	if (!currentUser.isModerator() && !comment.hasAuthor(currentUser.getCurrent())) {
		result.use(http()).sendError(403);
		return;
	}
	Class<?> type = urlMapping.getClassFor(onWhat);
	org.mamute.model.Post post = comments.loadCommentable(type, postId);
	post.deleteComment(comment);
	comments.delete(comment);
	result.use(Results.referer()).redirect();
}
 
Example #3
Source File: CORSController.java    From aprendendo-vraptor with Apache License 2.0 5 votes vote down vote up
@Options
@Path(value = "/*")
public void options(@Observes VRaptorRequestStarted requestInfo) {
		
	Set<HttpMethod> allowed = router.allowedMethodsFor(requestInfo.getRequest().getRequestedUri());
	String allowMethods = allowed.toString().replaceAll("\\[|\\]", "");
	
	result.use(Results.status()).header("Allow", allowMethods);
	result.use(Results.status()).header("Access-Control-Allow-Methods", allowMethods);
	result.use(Results.status()).header("Access-Control-Allow-Headers", "Content-Type, X-Requested-With, accept, Authorization, origin");
}
 
Example #4
Source File: VoteController.java    From mamute with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private void tryToVoteVotable(Long id, VoteType voteType, Class votableType) {
	try {
	    Votable votable = votes.loadVotable(votableType, id);
	    Vote current = new Vote(currentUser.getCurrent(), voteType);
	    votingMachine.register(votable, current, votableType);
	    votes.save(current);
	    result.use(Results.json()).withoutRoot().from(votable.getVoteCount()).serialize();
	} catch (IllegalArgumentException e) {
	    result.use(Results.http()).sendError(409);
	    return;
       }
}
 
Example #5
Source File: VoteController.java    From mamute with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
	private void tryToRemoveVoteVotable(Long id, VoteType voteType, Class votableType) {
		try {
		    Votable votable = votes.loadVotable(votableType, id);
		    Vote current = new Vote(currentUser.getCurrent(), voteType);
		    votingMachine.unRegister(votable, current, votableType);
//		    votes.save(current);
		    result.use(Results.json()).withoutRoot().from(votable.getVoteCount()).serialize();
		} catch (IllegalArgumentException e) {
		    result.use(Results.http()).sendError(409);
		    return;
        }
	}
 
Example #6
Source File: CommentController.java    From mamute with Apache License 2.0 5 votes vote down vote up
@Post
public void edit(Long id, MarkedText comment) {
	Comment original = comments.getById(id);
	if (!currentUser.getCurrent().isAuthorOf(original)) {
		result.use(Results.status()).badRequest("comment.edit.not_author");
		return;
	}
	if (validator.validate(comment.getPure())) {
		original.setComment(comment);
		comments.save(original);
		result.forwardTo(BrutalTemplatesController.class).comment(original);
	}
	validator.onErrorUse(Results.http()).setStatusCode(400);
}
 
Example #7
Source File: AnswerController.java    From mamute with Apache License 2.0 5 votes vote down vote up
@Post
public void markAsSolution(Long solutionId) {
	Answer solution = answers.getById(solutionId);
	Question question = solution.getMainThread();
       if (!currentUser.getCurrent().isAuthorOf(question)) {
		result.use(Results.status()).forbidden(bundle.getMessage("answer.error.not_autor"));
		result.redirectTo(QuestionController.class).showQuestion(question,
				question.getSluggedTitle());
		return;
	} 
       markOrRemoveSolution(solution);
       result.nothing();
}
 
Example #8
Source File: QuestionController.java    From mamute with Apache License 2.0 5 votes vote down vote up
@Post
@CustomBrutauthRules(EditQuestionRule.class)
public void edit(@Load Question original, String title, MarkedText description, String tagNames,
		String comment, List<Long> attachmentsIds) {

	List<String> splitedTags = splitter.splitTags(tagNames);
	List<Tag> loadedTags = tagsManager.findOrCreate(splitedTags);
	validate(loadedTags, splitedTags);

	QuestionInformation information = new QuestionInformation(title, description, this.currentUser, loadedTags, comment);
	brutalValidator.validate(information);
	UpdateStatus status = original.updateWith(information, new Updater(environmentKarma));

	validator.onErrorUse(Results.page()).of(this.getClass()).questionEditForm(original);

	result.include("editComment", comment);
	result.include("question", original);

	questions.save(original);
	List<Attachment> attachmentsLoaded = attachments.load(attachmentsIds);
	original.removeAttachments();
	original.add(attachmentsLoaded);
	index.indexQuestion(original);

	result.include("mamuteMessages",
			Arrays.asList(messageFactory.build("confirmation", status.getMessage())));
	result.redirectTo(this).showQuestion(original, original.getSluggedTitle());
}
 
Example #9
Source File: QuestionController.java    From mamute with Apache License 2.0 5 votes vote down vote up
@Post
@CustomBrutauthRules({LoggedRule.class, InputRule.class})
public void newQuestion(String title, MarkedText description, String tagNames, boolean watching,
						List<Long> attachmentsIds) {
	List<Attachment> attachments = this.attachments.load(attachmentsIds);
	attachmentsValidator.validate(attachments);
	List<String> splitedTags = splitter.splitTags(tagNames);

	List<Tag> foundTags = tagsManager.findOrCreate(splitedTags);
	validate(foundTags, splitedTags);

	QuestionInformation information = new QuestionInformation(title, description, currentUser, foundTags, "new question");
	brutalValidator.validate(information);
	User author = currentUser.getCurrent();
	Question question = new Question(information, author);
	result.include("question", question);
	validator.onErrorUse(Results.page()).of(this.getClass()).questionForm();

	questions.save(question);
	index.indexQuestion(question);
	question.add(attachments);

	ReputationEvent reputationEvent = new ReputationEvent(EventType.CREATED_QUESTION, question, author);
	author.increaseKarma(reputationEvent.getKarmaReward());
	reputationEvents.save(reputationEvent);
	if (watching) {
		watchers.add(question, new Watcher(author));
	}

	questionCreated.fire(new QuestionCreated(question));
	result.include("mamuteMessages", asList(messageFactory.build("alert", "question.quality_reminder")));
	result.redirectTo(this).showQuestion(question, question.getSluggedTitle());
}
 
Example #10
Source File: LoggedHandler.java    From mamute with Apache License 2.0 5 votes vote down vote up
@Override
public void handle() {
	if("application/json".equals(req.getHeader("Accept"))){
		result.use(Results.http()).body(bundle.getMessage("error.requires_login")).sendError(403);			
	}else{
		result.include("loginRequiredMessages", asList(messageFactory.build("alert", "auth.access.denied")));
		String redirectUrl = req.getRequestURL().toString();
		result.redirectTo(AuthController.class).loginForm(redirectUrl);
	}
}
 
Example #11
Source File: CORSController.java    From aprendendo-vraptor with Apache License 2.0 5 votes vote down vote up
@Options
@Path(value = "/*")
public void options(@Observes VRaptorRequestStarted requestInfo) {
		
	Set<HttpMethod> allowed = router.allowedMethodsFor(requestInfo.getRequest().getRequestedUri());
	String allowMethods = allowed.toString().replaceAll("\\[|\\]", "");
	
	result.use(Results.status()).header("Allow", allowMethods);
	result.use(Results.status()).header("Access-Control-Allow-Methods", allowMethods);
	result.use(Results.status()).header("Access-Control-Allow-Headers", "Content-Type, X-Requested-With, accept, Authorization, origin");
}
 
Example #12
Source File: ForwardToDefaultView.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
public void forward(@Observes RequestSucceded event) {
	if (result.used() || event.getResponse().isCommitted()) {
		logger.debug("Request already dispatched and commited somewhere else, not forwarding.");
		return;
	}
	
	logger.debug("forwarding to the dafault page for this logic");
	result.use(Results.page()).defaultView();
}
 
Example #13
Source File: DefaultResultTest.java    From vraptor4 with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldCallAssertAbsenceOfErrorsMethodFromMessages() throws Exception {
	result.use(Results.json());
	verify(messages).assertAbsenceOfErrors();
}
 
Example #14
Source File: MockHttpResultTest.java    From vraptor4 with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
	result.use(Results.http()).body("content");
	Assert.assertEquals("content", result.getBody());
}
 
Example #15
Source File: AbstractResult.java    From vraptor4 with Apache License 2.0 4 votes vote down vote up
@Override
public void nothing() {
	use(Results.nothing());
}
 
Example #16
Source File: DefaultInvalidInputHandler.java    From vraptor4 with Apache License 2.0 4 votes vote down vote up
@Override
public void deny(InvalidInputException e) {
	result.use(Results.http()).sendError(SC_BAD_REQUEST, e.getMessage());		
}
 
Example #17
Source File: InputHandler.java    From mamute with Apache License 2.0 4 votes vote down vote up
@Override
public void handle() {
	int remainingTime = input.getRemainingTime(user.getCurrent());
	String errorMessage = format(bundle.getMessage("error.input_overflow"), remainingTime);
	result.include("unauthorizedMessage", errorMessage).use(Results.http()).sendError(403);
}
 
Example #18
Source File: MessagesController.java    From mamute with Apache License 2.0 4 votes vote down vote up
@Get("/messages/loadAll")
public void loadMessages() {
	Map<String, String> messages = loader.getAllMessages();
	result.use(Results.json()).from(messages).serialize();
}