Example usage for org.springframework.web.bind MissingServletRequestParameterException MissingServletRequestParameterException

List of usage examples for org.springframework.web.bind MissingServletRequestParameterException MissingServletRequestParameterException

Introduction

In this page you can find the example usage for org.springframework.web.bind MissingServletRequestParameterException MissingServletRequestParameterException.

Prototype

public MissingServletRequestParameterException(String parameterName, String parameterType) 

Source Link

Document

Constructor for MissingServletRequestParameterException.

Usage

From source file:org.openinfobutton.responder.service.impl.ResponderServiceImpl.java

/**
 *
 * @param requestParameters// ww  w  .  j  a v  a  2s. co  m
 * @return
 * @throws MissingServletRequestParameterException
 */
@Override
@Transactional
public boolean requestContainsRequiredParameters(Map<String, String> requestParameters)
        throws MissingServletRequestParameterException {

    StringBuilder errorMessage = new StringBuilder();

    Collection<RequestParameter> requiredRequestParmeters = responderRequestParameterDao
            .getRequiredOpenInfobuttonRequestParameters();

    int i = 0;
    for (RequestParameter requiredRequestParmeter : requiredRequestParmeters) {
        if (!requestParameters.containsKey(requiredRequestParmeter.getParameterName())) {
            if (i > 1) {
                errorMessage.append(", ");
            }
            errorMessage.append(requiredRequestParmeter.getParameterName());
            i++;
        }
    }

    if (errorMessage.length() > 0) {

        String messagePrefix = null;
        String messageSuffix = null;

        if (i > 1) {
            messagePrefix = " are";
            messageSuffix = "s.";
        } else {
            messagePrefix = " is a";
            messageSuffix = ".";
        }

        throw new MissingServletRequestParameterException(errorMessage.toString(),
                messagePrefix + " required request parameter" + messageSuffix);
    }

    return true;

}

From source file:org.fao.geonet.api.registries.vocabularies.KeywordsApi.java

/**
 * Upload thesaurus./*from   w w w  . j a  v  a2 s  . com*/
 *
 * @param file the file
 * @param type the type
 * @param dir the dir
 * @param stylesheet the stylesheet
 * @param request the request
 * @return the element
 * @throws Exception the exception
 */
@ApiOperation(value = "Uploads a new thesaurus from a file", nickname = "uploadThesaurus", notes = "Uploads a new thesaurus.")
@RequestMapping(method = RequestMethod.POST, produces = MediaType.TEXT_XML_VALUE)
@ApiResponses(value = { @ApiResponse(code = 201, message = "Thesaurus uploaded in SKOS format."),
        @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_REVIEWER) })

@PreAuthorize("hasRole('Reviewer')")
@ResponseBody
@ResponseStatus(value = HttpStatus.CREATED)
public String uploadThesaurus(
        @ApiParam(value = "If set, do a file upload.") @RequestParam(value = "file", required = false) MultipartFile file,
        @ApiParam(value = "Local or external (default).") @RequestParam(value = "type", defaultValue = "external") String type,
        @ApiParam(value = "Type of thesaurus, usually one of the ISO thesaurus type codelist value. Default is theme.") @RequestParam(value = "dir", defaultValue = "theme") String dir,
        @ApiParam(value = "XSL to be use to convert the thesaurus before load. Default _none_.") @RequestParam(value = "stylesheet", defaultValue = "_none_") String stylesheet,
        HttpServletRequest request) throws Exception {

    long start = System.currentTimeMillis();
    ServiceContext context = ApiUtils.createServiceContext(request);

    // Different options for upload
    boolean fileUpload = file != null && !file.isEmpty();

    // Upload RDF file
    Path rdfFile = null;
    String fname = null;
    File tempDir = null;

    if (fileUpload) {

        Log.debug(Geonet.THESAURUS, "Uploading thesaurus file: " + file.getOriginalFilename());

        tempDir = Files.createTempDirectory("thesaurus").toFile();

        Path tempFilePath = tempDir.toPath().resolve(file.getOriginalFilename());
        File convFile = tempFilePath.toFile();
        file.transferTo(convFile);

        rdfFile = convFile.toPath();
        fname = file.getOriginalFilename();
    } else {

        Log.debug(Geonet.THESAURUS, "No file provided for thesaurus upload.");
        throw new MissingServletRequestParameterException("Thesaurus source not provided", "file");
    }

    try {
        if (StringUtils.isEmpty(fname)) {
            throw new Exception("File upload from URL or file return null");
        }

        long fsize;
        if (rdfFile != null && Files.exists(rdfFile)) {
            fsize = Files.size(rdfFile);
        } else {
            throw new MissingServletRequestParameterException("Thesaurus file doesn't exist", "file");
        }

        // -- check that the archive actually has something in it
        if (fsize == 0) {
            throw new MissingServletRequestParameterException("Thesaurus file has zero size", "file");
        }

        String extension = FilenameUtils.getExtension(fname);

        if (extension.equalsIgnoreCase("rdf") || extension.equalsIgnoreCase("xml")) {
            Log.debug(Geonet.THESAURUS, "Uploading thesaurus: " + fname);

            // Rename .xml to .rdf for all thesaurus
            fname = fname.replace(extension, "rdf");
            uploadThesaurus(rdfFile, stylesheet, context, fname, type, dir);
        } else {
            Log.debug(Geonet.THESAURUS, "Incorrect extension for thesaurus named: " + fname);
            throw new Exception("Incorrect extension for thesaurus named: " + fname);
        }

        long end = System.currentTimeMillis();
        long duration = (end - start) / 1000;

        return String.format("Thesaurus '%s' loaded in %d sec.", fname, duration);
    } finally {
        if (tempDir != null) {
            FileUtils.deleteQuietly(tempDir);
        }
    }
}

From source file:org.fao.geonet.api.registries.vocabularies.KeywordsApi.java

/**
 * Upload thesaurus./*from w  ww  .  j a  v a 2s  .c o m*/
 *
 * @param url the url
 * @param registryUrl the registry url
 * @param registryLanguage the languages to retrieve from the registry
 * @param type the type
 * @param dir the dir
 * @param stylesheet the stylesheet
 * @param request the request
 * @return the element
 * @throws Exception the exception
 */
@ApiOperation(value = "Uploads a new thesaurus from URL or Registry", nickname = "uploadThesaurusFromUrl", notes = "Uploads a new thesaurus.")
@RequestMapping(method = RequestMethod.PUT, produces = MediaType.TEXT_XML_VALUE)
@ApiResponses(value = { @ApiResponse(code = 201, message = "Thesaurus uploaded in SKOS format."),
        @ApiResponse(code = 403, message = ApiParams.API_RESPONSE_NOT_ALLOWED_ONLY_REVIEWER) })
@PreAuthorize("hasRole('Reviewer')")
@ResponseBody
@ResponseStatus(value = HttpStatus.CREATED)
public String uploadThesaurusFromUrl(
        @ApiParam(value = "If set, try to download from the Internet.") @RequestParam(value = "url", required = false) String url,
        @ApiParam(value = "If set, try to download from a registry.") @RequestParam(value = "registryUrl", required = false) String registryUrl,
        @ApiParam(value = "Languages to download from a registry.") @RequestParam(value = "registryLanguage", required = false) String[] registryLanguage,
        @ApiParam(value = "Local or external (default).") @RequestParam(value = "type", defaultValue = "external") String type,
        @ApiParam(value = "Type of thesaurus, usually one of the ISO thesaurus type codelist value. Default is theme.") @RequestParam(value = "dir", defaultValue = "theme") String dir,
        @ApiParam(value = "XSL to be use to convert the thesaurus before load. Default _none_.") @RequestParam(value = "stylesheet", defaultValue = "_none_") String stylesheet,
        HttpServletRequest request) throws Exception {

    long start = System.currentTimeMillis();
    ServiceContext context = ApiUtils.createServiceContext(request);

    boolean urlUpload = !StringUtils.isEmpty(url);
    boolean registryUpload = !StringUtils.isEmpty(registryUrl);

    // Upload RDF file
    Path rdfFile = null;
    String fname = null;

    // Specific upload steps
    if (urlUpload) {

        Log.debug(Geonet.THESAURUS, "Uploading thesaurus from URL: " + url);

        rdfFile = getXMLContentFromUrl(url, context);
        fname = url.substring(url.lastIndexOf("/") + 1, url.length()).replaceAll("\\s+", "");

        // File with no extension in URL
        if (fname.lastIndexOf('.') == -1) {
            fname += ".rdf";
        }

    } else if (registryUpload) {
        if (ArrayUtils.isEmpty(registryLanguage)) {
            throw new MissingServletRequestParameterException("Select at least one language.", "language");
        }

        Log.debug(Geonet.THESAURUS, "Uploading thesaurus from registry : " + registryUrl);

        String itemName = registryUrl.substring((registryUrl.lastIndexOf("/") + 1), registryUrl.length());

        rdfFile = extractSKOSFromRegistry(registryUrl, itemName, registryLanguage, context);
        fname = registryUrl.replaceAll("[^A-Za-z]+", "") + "-" + itemName + ".rdf";

    } else {

        Log.debug(Geonet.THESAURUS, "No URL or file name provided for thesaurus upload.");
        throw new MissingServletRequestParameterException("Thesaurus source not provided", "url");

    }

    if (StringUtils.isEmpty(fname)) {
        throw new ResourceNotFoundException("File upload from URL or file return null");
    }

    long fsize;
    if (rdfFile != null && Files.exists(rdfFile)) {
        fsize = Files.size(rdfFile);
    } else {
        throw new ResourceNotFoundException("Thesaurus file doesn't exist");
    }

    // -- check that the archive actually has something in it
    if (fsize == 0) {
        throw new ResourceNotFoundException("Thesaurus file has zero size");
    }

    String extension = FilenameUtils.getExtension(fname);

    if (extension.equalsIgnoreCase("rdf") || extension.equalsIgnoreCase("xml")) {
        Log.debug(Geonet.THESAURUS, "Uploading thesaurus: " + fname);

        // Rename .xml to .rdf for all thesaurus
        fname = fname.replace(extension, "rdf");
        uploadThesaurus(rdfFile, stylesheet, context, fname, type, dir);
    } else {
        Log.debug(Geonet.THESAURUS, "Incorrect extension for thesaurus named: " + fname);
        throw new MissingServletRequestParameterException("Incorrect extension for thesaurus", fname);
    }

    long end = System.currentTimeMillis();
    long duration = (end - start) / 1000;

    return String.format("Thesaurus '%s' loaded in %d sec.", fname, duration);
}