br.com.caelum.vraptor.Post Java Examples

The following examples show how to use br.com.caelum.vraptor.Post. 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: MusicController.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
/**
 * Accepts HTTP POST requests.
 * 
 * URL:  /musics
 * View: /WEB-INF/jsp/music/add.jsp
 *
 * The method adds a new music and updates the user.
 * We use POST HTTP verb when we want to create some resource.
 *
 * The <code>UploadedFile</code> is automatically handled
 * by VRaptor's <code>MultipartInterceptor</code>.
 */
@Path("/musics")
@Post
public void add(final @NotNull @Valid Music music, UploadedFile file) {
	validator.onErrorForwardTo(UsersController.class).home();

	musicDao.add(music);
	
	User currentUser = userInfo.getUser();
	userDao.refresh(currentUser);
	
	currentUser.add(music);
	
	// is there a file?
	if (file != null) {
	    // Let's save the file
		musics.save(file, music);
		logger.info("Uploaded file: {}", file.getFileName());
	}

	// you can add objects to result even in redirects. Added objects will
	// survive one more request when redirecting.
	result.include("notice", music.getTitle() + " music added");

	result.redirectTo(UsersController.class).home();
}
 
Example #2
Source File: SignupController.java    From mamute with Apache License 2.0 6 votes vote down vote up
@Post
public void signup(String email, String password, SanitizedText name, String passwordConfirmation) {
	checkSignup();

	User newUser = new User(name, email);
	LoginMethod brutalLogin = LoginMethod.brutalLogin(newUser, email, password);
	newUser.add(brutalLogin);
	
	validator.validate(newUser, password, passwordConfirmation);
	result.include("email", email);
	result.include("name", name);
	validator.onErrorRedirectTo(this).signupForm();
	
    users.save(newUser);
    loginMethods.save(brutalLogin);
    result.include("mamuteMessages", asList(messageFactory.build("confirmation", "signup.confirmation")));
    linker.linkTo(ListController.class).home(null);
    result.forwardTo(AuthController.class).login(email, password, linker.get());
}
 
Example #3
Source File: AnswerController.java    From mamute with Apache License 2.0 6 votes vote down vote up
@Post
@CustomBrutauthRules(EditAnswerRule.class)
public void edit(@Load Answer original, MarkedText description, String comment, List<Long> attachmentsIds) {
	AnswerInformation information = new AnswerInformation(description, currentUser, comment);
	brutalValidator.validate(information);
	
	validator.onErrorRedirectTo(this).answerEditForm(original);
	
	UpdateStatus status = original.updateWith(information, new Updater(environmentKarma));
	answers.save(original);
	List<Attachment> attachmentsLoaded = attachments.load(attachmentsIds);

	original.replace(attachmentsLoaded);

	result.include("mamuteMessages", Arrays.asList(messageFactory.build("confirmation", status.getMessage())));
	Question originalQuestion = original.getMainThread();
	result.redirectTo(QuestionController.class).showQuestion(originalQuestion, originalQuestion.getSluggedTitle());
}
 
Example #4
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 #5
Source File: UserProfileController.java    From mamute with Apache License 2.0 6 votes vote down vote up
@CustomBrutauthRules(LoggedRule.class)
@Post
public void uploadAvatar(UploadedFile avatar, @Load User user) {
	Attachment attachment = null;
	try {
		attachment = imageStore.processAndStore(avatar, user, clientIp);
	} catch (IOException e) {
		result.use(http()).sendError(400);
		return;
	}

	Attachment old = user.getAvatar();
	if (old != null) {
		imageStore.delete(old);
	}

	user.setAvatar(attachment);
	result.use(json()).withoutRoot().from(attachment).serialize();
}
 
Example #6
Source File: AuthController.java    From mamute with Apache License 2.0 6 votes vote down vote up
@Public
@Post
public void login(String email, String password, String redirectUrl) {
	try {
		if (validator.validate(email, password) && auth.authenticate(email, password)) {
			redirectToRightUrl(redirectUrl);
		} else {
			includeAsList("mamuteMessages", i18n("error", "auth.invalid.login"));
			validator.onErrorRedirectTo(this).loginForm(redirectUrl);
			redirectTo(this).loginForm(redirectUrl);
		}
	} catch (AuthenticationException e) {
		includeAsList("mamuteMessages", i18n("error", "auth.configuration.error", e.getAuthType()));
		validator.onErrorRedirectTo(this).loginForm(redirectUrl);
		redirectTo(this).loginForm(redirectUrl);
	}
}
 
Example #7
Source File: ForgotPasswordController.java    From mamute with Apache License 2.0 6 votes vote down vote up
@Post
public void changePassword(Long id, String token, String password, String passwordConfirmation) {
	User user = validateTokenAndGetUser(id, token);

	boolean passwordUpdated = user.updateForgottenPassword(password, passwordConfirmation);
	if(!passwordUpdated) {
		validator.add(messageFactory.build("error", "forgot_password.password_doesnt_match"));
		validator.onErrorRedirectTo(this).forgotPasswordForm();
	}
	
	user.touchForgotPasswordToken();
	users.save(user);
	result.include("mamuteMessages", Arrays.asList(
				messageFactory.build("confirmation", "forgot_password.password_changed")
			));
	linker.linkTo(ListController.class).home(null);
    result.forwardTo(AuthController.class).login(user.getEmail(), password, linker.get());
}
 
Example #8
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 #9
Source File: ForgotPasswordController.java    From mamute with Apache License 2.0 6 votes vote down vote up
@Post
public void requestEmailWithToken(String email) {
	User user = users.loadByEmail(email);

	if (user == null) {
		validator.add(messageFactory.build("error", "forgot_password.invalid_email"));
		validator.onErrorRedirectTo(this).forgotPasswordForm();
		return;
	}

	Email forgotPasswordEmail = emailWithTokenFor(user);
	try {
		mailer.send(forgotPasswordEmail);
		result.include("mamuteMessages", Arrays.asList(
					messageFactory.build("confirmation", "forgot_password.sent_mail", user.getEmail()),
					messageFactory.build("confirmation", "forgot_password.sent_mail.warn")
				));
		result.redirectTo(this).forgotPasswordForm();
	} catch (EmailException e) {
		validator.add(messageFactory.build("error", "forgot_password.send_mail.error"));
		validator.onErrorRedirectTo(this).forgotPasswordForm();
	}	
}
 
Example #10
Source File: OccurrenceStateController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("insert")
public void insert(
	@Valid 
	@Unique(identityPropertyName="id" , propertyName="name" , entityClass=OccurrenceState.class)
	OccurrenceState occurrenceState, Validator validator) {
	validator.onErrorForwardTo( this.getClass() ).form();

	occurrenceStateBean.insert(occurrenceState);
	result.include("message", messagesBundle.getString("insert.success"));
	result.redirectTo( this.getClass() ).list();
}
 
Example #11
Source File: NewsController.java    From mamute with Apache License 2.0 5 votes vote down vote up
@Post
@CustomBrutauthRules({LoggedRule.class, InputRule.class})
public void newNews(String title, MarkedText description) {
	NewsInformation information = new NewsInformation(title, description, currentUser, "new news");
	User author = currentUser.getCurrent();
	News news = new News(information, author);
	result.include("news", news);
	newses.save(news);
	result.redirectTo(this).showNews(news, news.getSluggedTitle());
}
 
Example #12
Source File: ContatoController.java    From aprendendo-vraptor with Apache License 2.0 5 votes vote down vote up
@Post
@Path(value = {"/", ""})
@Consumes(value = "application/json", options = WithoutRoot.class)
public void novo( Contato contato ) {
	setPhone(contato);
	contatos.novo(contato);
	result.use(status()).created();
}
 
Example #13
Source File: CostCenterController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("insert")
public void insert(
        @Valid
        @Unique(propertyName = "name", identityPropertyName = "id", entityClass = CostCenter.class)
        CostCenter costCenter, Validator validator) {
    validator.onErrorForwardTo(CostCenterController.class).form();

    costCenterBean.insert(costCenter);
    result.include("message", messagesBundle.getString("insert.success"));
    result.redirectTo(CostCenterController.class).list();
}
 
Example #14
Source File: CostCenterController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("update")
public void update(@Valid
                   @Unique(propertyName = "name", identityPropertyName = "id", entityClass = CostCenter.class)
                   CostCenter costCenter, Validator validator) {
    validator.onErrorForwardTo(CostCenterController.class).form();

    costCenterBean.update(costCenter);
    result.include("message", messagesBundle.getString("update.success"));
    result.redirectTo(CostCenterController.class).list();
}
 
Example #15
Source File: ManufacturerController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post
@Path("insert")
public void insert(@Valid Manufacturer manufacturer, Validator validator) {
    validator.onErrorRedirectTo(ManufacturerController.class).form();

    manufacturerBean.insert(manufacturer);
    result.include("message", messagesBundle.getString("insert.success"));
    result.redirectTo("/manufacturer/list");
}
 
Example #16
Source File: ManufacturerController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post
@Path("update")
public void update(@Valid Manufacturer manufacturer, Validator validator) {
    validator.onErrorRedirectTo(ManufacturerController.class).form();

    manufacturerBean.update(manufacturer);
    result.include("message", messagesBundle.getString("update.success"));
    result.redirectTo(ManufacturerController.class).list();
}
 
Example #17
Source File: UserController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post
@Path("insert")
public void insert(@Valid User user, String passwordConfirmation, Validator validator) {
    verifyPasswordConfirmation(user, passwordConfirmation, validator);
    validator.onErrorForwardTo(UserController.class).form();

    userBean.insert(user);
    result.include("message", messagesBundle.getString("insert.success"));
    result.redirectTo("/user/list");
}
 
Example #18
Source File: OccurrenceStateController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("update")
public void update(
	@Valid
	@Unique(identityPropertyName="id" , propertyName="name" , entityClass=OccurrenceState.class)
	OccurrenceState occurrenceState, Validator validator) {
	validator.onErrorForwardTo(this.getClass()).form();

	occurrenceStateBean.update(occurrenceState);
	result.include("message", messagesBundle.getString("update.success"));
	result.redirectTo(this.getClass()).list();
}
 
Example #19
Source File: UserController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post
@Path("update")
public void update(@Valid User user, String passwordConfirmation, Validator validator) {
    verifyPasswordConfirmation(user, passwordConfirmation, validator);
    validator.onErrorForwardTo(UserController.class).form();

    userBean.update(user);
    result.include("message", messagesBundle.getString("update.success"));
    result.redirectTo(UserController.class).list();
}
 
Example #20
Source File: ProblemTypeController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("insert")
public void insert(
        @Valid
        @Unique(identityPropertyName = "id", propertyName = "name", entityClass = ProblemType.class)
        ProblemType problemType, Validator validator) {

    validator.onErrorForwardTo(this).form();

    problemTypeBean.insert(problemType);

    result.include("message", messagesBundle.getString("insert.success"));
    result.redirectTo(this).list();
}
 
Example #21
Source File: ProblemTypeController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post
@Path("update")
public void update(
        @Valid
        @Unique(identityPropertyName = "id", propertyName = "name", entityClass = ProblemType.class)
        ProblemType problemType, Validator validator) {
    validator.onErrorForwardTo(this).form();

    problemTypeBean.update(problemType);

    result.include("message", messagesBundle.getString("update.success"));
    result.redirectTo(this).list();
}
 
Example #22
Source File: EquipmentModelController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("insert")
public void insert(@Valid
                   @Unique(propertyName = "name", identityPropertyName = "id", entityClass = EquipmentModel.class)
                   EquipmentModel equipmentModel, Validator validator) {
    verifyIfSelectedEquipmentType(equipmentModel, validator);
    validator.onErrorForwardTo(EquipmentModelController.class).form();

    equipmentModelBean.insert(equipmentModel);
    result.include("message", messagesBundle.getString("insert.success"));
    result.redirectTo(EquipmentModelController.class).list();
}
 
Example #23
Source File: EquipmentModelController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("update")
public void update(@Valid
                   @Unique(propertyName = "name", identityPropertyName = "id", entityClass = EquipmentModel.class)
                   EquipmentModel equipmentModel, Validator validator) {
    verifyIfSelectedEquipmentType(equipmentModel, validator);
    validator.onErrorForwardTo(EquipmentModelController.class).form();

    equipmentModelBean.update(equipmentModel);
    result.include("message", messagesBundle.getString("update.success"));
    result.redirectTo(EquipmentModelController.class).list();
}
 
Example #24
Source File: SectorController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("/insert")
public void insert(@Valid Sector sector, Validator validator) {
    validator.onErrorForwardTo(SectorController.class).form();

    sectorBean.insert(sector);
    result.include("message", messagesBundle.getString("insert.success"));
    result.redirectTo(SectorController.class).list();
}
 
Example #25
Source File: SectorController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post
@Path("update")
public void update(@Valid Sector sector, Validator validator) {
    validator.onErrorForwardTo(SectorController.class).form();

    sectorBean.update(sector);
    result.include("message", messagesBundle.getString("update.success"));
    result.redirectTo(SectorController.class).list();
}
 
Example #26
Source File: UpdateOccurrenceController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post
@Path("/{occurrenceId}")
public void processForm(Long occurrenceId, String updateNote, @Valid Occurrence occurrence, Validator validator) {
	validator.onErrorForwardTo(OpenOccurrenceController.class).form();
	
	//TODO Add user from the session
	User user = userBean.findAll().get(0);
	try {
		occurrenceBean.updateOccurrence( occurrence  , updateNote , user);
		result.include("message" , messageBundle.getString("updateOccurrence.messageSuccess") );
	} catch (NoChangeInOccurrenceException e) {
		result.include("errorMessage" , messageBundle.getString("updateOccurrence.messageError") );
	}
	result.forwardTo(this.getClass()).form( occurrenceId );
}
 
Example #27
Source File: EquipmentTypeController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("insert")
public void insert(@Valid EquipmentType equipmentType, Validator validator) {
    validator.onErrorForwardTo(EquipmentTypeController.class).form();

    equipmentTypeBean.insert(equipmentType);
    result.include("message", messagesBundle.getString("insert.success"));
    result.redirectTo(EquipmentTypeController.class).list();
}
 
Example #28
Source File: EquipmentTypeController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("update")
public void update(@Valid EquipmentType equipmentType, Validator validator) {
    validator.onErrorForwardTo(EquipmentTypeController.class).form();

    equipmentTypeBean.update(equipmentType);
    result.include("message", messagesBundle.getString("update.success"));
    result.redirectTo(EquipmentTypeController.class).list();
}
 
Example #29
Source File: EquipmentController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post("insert")
public void insert(@Valid  
		@Unique(propertyName = "serialId" , identityPropertyName="id" , entityClass =Equipment.class )
		Equipment equipment, Validator validator ) {
	
	verifyIfSelectedEquipmentModel(equipment, validator);
	validator.onErrorForwardTo( EquipmentController.class ).form();
	equipmentBean.insert(equipment);
	
    result.include("message", messagesBundle.getString("insert.success") );
    result.redirectTo(EquipmentController.class).list();
}
 
Example #30
Source File: EquipmentController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Post
@Path("update")
public void update(@Valid 
		@Unique(propertyName = "serialId" , identityPropertyName="id" , entityClass =Equipment.class )
		Equipment equipment, Validator validator) {
	
	verifyIfSelectedEquipmentModel(equipment, validator);
	validator.onErrorForwardTo( EquipmentController.class ).form();       
    
	equipmentBean.update(equipment);
	result.redirectTo(EquipmentController.class).list();
}