Example #1
0
  @Test
  public void testConfiguration_Count_Exclude_NoRegexp() throws Exception {
    String[] args = new String[] {"-c", "-v", "./src/test/resources/samples/000.txt"};

    try {
      new Grep(null, args);
      fail("should fail, combination does not make sense");
    } catch (UserErrorException e) {
      log.info(e.getMessage());
    }
  }
  @RequestMapping(value = "/addphoto.jsp", method = RequestMethod.POST)
  @PreAuthorize("hasRole('ROLE_ANONYMOUS')")
  public ModelAndView addPhoto(
      @RequestParam("file") MultipartFile file, HttpServletResponse response) throws Exception {

    if (file == null || file.isEmpty()) {
      return new ModelAndView("addphoto", "error", "изображение не задано");
    }

    try {
      File uploadedFile =
          File.createTempFile(
              "userpic", "", new File(siteConfig.getPathPrefix() + "/linux-storage/tmp/"));

      file.transferTo(uploadedFile);

      ImageParam param = userService.checkUserPic(uploadedFile);
      String extension = param.getExtension();

      Random random = new Random();

      String photoname;
      File photofile;

      do {
        photoname =
            Integer.toString(AuthUtil.getCurrentUser().getId())
                + ':'
                + random.nextInt()
                + '.'
                + extension;
        photofile = new File(siteConfig.getHTMLPathPrefix() + "/photos", photoname);
      } while (photofile.exists());

      if (!uploadedFile.renameTo(photofile)) {
        logger.warn("Can't move photo to " + photofile);
        throw new ScriptErrorException("Can't move photo: internal error");
      }

      userDao.setPhoto(AuthUtil.getCurrentUser(), photoname);

      logger.info("Установлена фотография пользователем " + AuthUtil.getCurrentUser().getNick());

      return new ModelAndView(
          new RedirectView(
              UriComponentsBuilder.fromUri(
                      PROFILE_NOCACHE_URI_TEMPLATE.expand(AuthUtil.getCurrentUser().getNick()))
                  .queryParam("nocache", Integer.toString(random.nextInt()) + '=')
                  .build()
                  .encode()
                  .toString()));
    } catch (IOException ex) {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return new ModelAndView("addphoto", "error", ex.getMessage());
    } catch (BadImageException ex) {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return new ModelAndView("addphoto", "error", ex.getMessage());
    } catch (UserErrorException ex) {
      response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return new ModelAndView("addphoto", "error", ex.getMessage());
    }
  }