Example usage for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentRequest

List of usage examples for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentRequest

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support ServletUriComponentsBuilder fromCurrentRequest.

Prototype

public static ServletUriComponentsBuilder fromCurrentRequest() 

Source Link

Document

Same as #fromRequest(HttpServletRequest) except the request is obtained through RequestContextHolder .

Usage

From source file:com.onyxscheduler.web.JobController.java

@RequestMapping(value = "/groups/{group}/jobs", method = RequestMethod.POST)
public ResponseEntity<Job> addJob(@PathVariable String group, @RequestBody Job job)
        throws NonMatchingGroupsException, Scheduler.DuplicateJobKeyException {
    if (job.getGroup() != null && !job.getGroup().equals(group)) {
        //throwing an exception instead of ResponseEntity.badRequest().build() to have a proper description of the bad request
        throw new NonMatchingGroupsException(group, job.getGroup());
    }//from ww  w  .  ja  v a  2 s  . com
    job.setGroup(group);
    scheduler.scheduleJob(job);
    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{job}").buildAndExpand(job.getName())
            .toUri();
    return ResponseEntity.created(location).body(job);
}

From source file:ch.wisv.areafiftylan.users.controller.UserRestController.java

/**
 * This method accepts POST requests on /users. It will send the input to the {@link UserService} to create a new
 * user/*from w  w  w .  ja  va2  s . c o  m*/
 *
 * @param input The user that has to be created. It consists of 3 fields. The username, the email and the plain-text
 *              password. The password is saved hashed using the BCryptPasswordEncoder
 *
 * @return The generated object, in JSON format.
 */
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> add(HttpServletRequest request, @Validated @RequestBody UserDTO input) {
    User save = userService.create(input, request);

    // Create headers to set the location of the created User object.
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(save.getId()).toUri());

    return createResponseEntity(HttpStatus.CREATED, httpHeaders,
            "User successfully created at " + httpHeaders.getLocation(), save);
}

From source file:de.lgblaumeiser.ptm.rest.BookingRestController.java

@RequestMapping(method = RequestMethod.POST, value = "/{dayString}")
public ResponseEntity<?> addBooking(@PathVariable String dayString, @RequestBody BookingBody newData) {
    LocalDate day = LocalDate.parse(dayString);
    Activity activity = services.activityStore().retrieveById(valueOf(newData.activityId))
            .orElseThrow(IllegalStateException::new);
    Booking newBooking = services.bookingService().addBooking(day, newData.user, activity,
            parse(newData.starttime), newData.comment);
    if (newData.endtime != null) {
        newBooking = services.bookingService().endBooking(newBooking, parse(newData.endtime));
    }//from   ww w .  j av  a 2  s . c o m
    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{day}/{id}")
            .buildAndExpand(day.format(ISO_LOCAL_DATE), newBooking.getId()).toUri();
    return ResponseEntity.created(location).build();
}

From source file:com.gazbert.bxbot.rest.api.MarketConfigController.java

@RequestMapping(value = "/market/{marketId}", method = RequestMethod.PUT)
ResponseEntity<?> updateMarket(@AuthenticationPrincipal User user, @PathVariable String marketId,
        @RequestBody MarketConfig config) {

    final MarketConfig updatedMarketConfig = marketConfigService.updateMarket(config);
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{marketId}")
            .buildAndExpand(updatedMarketConfig.getId()).toUri());
    return new ResponseEntity<>(updatedMarketConfig, httpHeaders, HttpStatus.NO_CONTENT);
}

From source file:eu.supersede.dm.rest.CriteriaRest.java

/**
 * Create a new criterion.//  www . ja v a 2s .  c  o  m
 * @param vc
 */
@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<?> createCriteria(@RequestBody ValutationCriteria vc) {
    vc.setCriteriaId(null);
    DMGame.get().getJpa().criteria.save(vc);
    //        valutationCriterias.save(vc);

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(vc.getCriteriaId()).toUri());
    return new ResponseEntity<>(null, httpHeaders, HttpStatus.CREATED);
}

From source file:com.gazbert.bxbot.rest.api.EngineConfigController.java

/**
 * Updates Engine configuration for the bot.
 *
 * @return 204 'No Content' HTTP status code if engine config was updated successfully, some other HTTP status code otherwise.
 *//*from   w  ww .  ja  va2s  .  c  o m*/
@RequestMapping(value = "/engine", method = RequestMethod.PUT)
public ResponseEntity<?> updateEngine(@AuthenticationPrincipal User user, @RequestBody EngineConfig config) {

    engineConfigService.updateConfig(config);
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders
            .setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/").buildAndExpand().toUri());
    return new ResponseEntity<>(null, httpHeaders, HttpStatus.NO_CONTENT);
}

From source file:com.gazbert.bxbot.rest.api.MarketConfigController.java

@RequestMapping(value = "/market/{marketId}", method = RequestMethod.POST)
ResponseEntity<?> createMarket(@AuthenticationPrincipal User user, @PathVariable String marketId,
        @RequestBody MarketConfig config) {

    final MarketConfig updatedMarketConfig = marketConfigService.createMarket(config);
    final HttpHeaders httpHeaders = new HttpHeaders();

    httpHeaders.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{marketId}")
            .buildAndExpand(updatedMarketConfig.getId()).toUri());
    return new ResponseEntity<>(null, httpHeaders, HttpStatus.CREATED);
}

From source file:com.gazbert.bxbot.rest.api.ExchangeConfigController.java

/**
 * Updates Exchange configuration for the bot.
 *
 * @return 204 'No Content' HTTP status code if exchange config was updated, some other HTTP status code otherwise.
 *///from   w  w w .j a v a2s.c o  m
@RequestMapping(value = "/exchange", method = RequestMethod.PUT)
ResponseEntity<?> updateExchange(@AuthenticationPrincipal User user, @RequestBody ExchangeConfig config) {

    exchangeConfigService.updateConfig(config);
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders
            .setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/").buildAndExpand().toUri());
    return new ResponseEntity<>(null, httpHeaders, HttpStatus.NO_CONTENT);
}

From source file:com.gazbert.bxbot.rest.api.EmailAlertsConfigController.java

/**
 * Updates Email Alerts configuration for the bot.
 *
 * @return 204 'No Content' HTTP status code if Email Alerts config was updated, some other HTTP status code otherwise.
 *///from  w w w. j  ava2 s  .c  o  m
@RequestMapping(value = "/emailalerts", method = RequestMethod.PUT)
ResponseEntity<?> updateEmailAlerts(@AuthenticationPrincipal User user, @RequestBody EmailAlertsConfig config) {

    emailAlertsConfigService.updateConfig(config);
    final HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders
            .setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/").buildAndExpand().toUri());
    return new ResponseEntity<>(null, httpHeaders, HttpStatus.NO_CONTENT);
}

From source file:ch.wisv.areafiftylan.products.controller.OrderRestController.java

/**
 * When a User does a POST request to /orders, a new Order is created. The requestbody is a TicketDTO, so an order
 * always contains at least one ticket. Optional next tickets should be added to the order by POSTing to the
 * location provided./*from   ww w  .ja  va  2  s.c  o  m*/
 *
 * @param auth      The User that is currently logged in
 * @param ticketDTO Object containing information about the Ticket that is being ordered.
 *
 * @return A message informing about the result of the request
 */
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/orders", method = RequestMethod.POST)
@JsonView(View.OrderOverview.class)
public ResponseEntity<?> createOrder(Authentication auth, @RequestBody @Validated TicketDTO ticketDTO) {
    HttpHeaders headers = new HttpHeaders();
    User user = (User) auth.getPrincipal();

    // You can't buy non-buyable Tickts for yourself, this should be done via the createAdminOrder() method.
    if (!ticketDTO.getType().isBuyable()) {
        return createResponseEntity(HttpStatus.FORBIDDEN,
                "Can't order tickets with type " + ticketDTO.getType().getText());
    }

    Order order = orderService.create(user.getId(), ticketDTO);

    headers.setLocation(ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(order.getId()).toUri());

    return createResponseEntity(HttpStatus.CREATED, headers,
            "Ticket available and order successfully created at " + headers.getLocation(), order);
}