Java Code Examples for javax.servlet.http.HttpServletResponse#SC_NOT_ACCEPTABLE

The following examples show how to use javax.servlet.http.HttpServletResponse#SC_NOT_ACCEPTABLE . 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: JnlpLauncher.java    From weasis-pacs-connector with Eclipse Public License 2.0 6 votes vote down vote up
protected void parseLauncherTemplate(JnlpTemplate launcher) throws ServletErrorException, IOException {

        // Parse JNLP launcher as JDOM
        Element rootElt = null;

        // Assume the template has UTF-8 encoding
        try (BufferedReader reader =
            new BufferedReader(new InputStreamReader(launcher.realPathURL.toURL().openConnection().getInputStream(),
                StandardCharsets.UTF_8))) {

            rootElt = new SAXBuilder(XMLReaders.NONVALIDATING, null, null).build(reader).getRootElement();
        } catch (JDOMException e) {
            throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Can't parse launcher template", e);
        }

        if (!rootElt.getName().equals(JNLP_TAG_ELT_ROOT)) {
            throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Invalid JNLP launcher template");
        }

        launcher.rootElt = rootElt;
    }
 
Example 2
Source File: JnlpLauncher.java    From weasis-pacs-connector with Eclipse Public License 2.0 6 votes vote down vote up
protected void handleRequestArgumentParameter(JnlpTemplate launcher) throws ServletErrorException {
    String[] argValues = ServletUtil.getParameters(launcher.parameterMap.remove(WeasisConfig.PARAM_ARGUMENT));

    if (launcher.rootElt != null && argValues != null) {
        try {
            Element applicationDescElt = launcher.rootElt.getChild(JNLP_TAG_ELT_APPLICATION_DESC);
            if (applicationDescElt == null) {
                throw new IllegalStateException("JNLP TAG : <application-desc> is not found");
            } else {
                for (String newContent : argValues) {
                    applicationDescElt.addContent(new Element(JNLP_TAG_ELT_ARGUMENT).addContent(newContent));
                }
            }
        } catch (Exception e) {
            throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE,
                "Can't add argument parameter to launcher template ", e);
        }
    }
}
 
Example 3
Source File: JaxRsWebApplicationExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] dataProviderForShouldHandleException() {
    return new Object[][] {
        { new NotFoundException(), testProjectApiErrors.getNotFoundApiError() },
        { new WebApplicationException(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), testProjectApiErrors.getUnsupportedMediaTypeApiError() },
        { new WebApplicationException(HttpServletResponse.SC_METHOD_NOT_ALLOWED), testProjectApiErrors.getMethodNotAllowedApiError() },
        { new WebApplicationException(HttpServletResponse.SC_UNAUTHORIZED), testProjectApiErrors.getUnauthorizedApiError() },
        { new WebApplicationException(HttpServletResponse.SC_NOT_ACCEPTABLE), testProjectApiErrors.getNoAcceptableRepresentationApiError() },
        { mock(JsonProcessingException.class), testProjectApiErrors.getMalformedRequestApiError() }
    };
}
 
Example 4
Source File: Jersey1WebApplicationExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@DataProvider
public static Object[][] dataProviderForShouldHandleException() {
    return new Object[][] {
        { new NotFoundException(), testProjectApiErrors.getNotFoundApiError() },
        { mock(ParamException.URIParamException.class), testProjectApiErrors.getNotFoundApiError() },
        { mock(ParamException.class), testProjectApiErrors.getMalformedRequestApiError() },
        { new WebApplicationException(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), testProjectApiErrors.getUnsupportedMediaTypeApiError() },
        { new WebApplicationException(HttpServletResponse.SC_METHOD_NOT_ALLOWED), testProjectApiErrors.getMethodNotAllowedApiError() },
        { new WebApplicationException(HttpServletResponse.SC_UNAUTHORIZED), testProjectApiErrors.getUnauthorizedApiError() },
        { new WebApplicationException(HttpServletResponse.SC_NOT_ACCEPTABLE), testProjectApiErrors.getNoAcceptableRepresentationApiError() },
        { mock(JsonProcessingException.class), testProjectApiErrors.getMalformedRequestApiError() }
    };
}
 
Example 5
Source File: JnlpLauncher.java    From weasis-pacs-connector with Eclipse Public License 2.0 5 votes vote down vote up
protected void filterRequestParameterMarker(JnlpTemplate launcher) throws ServletErrorException {

        if (launcher != null && launcher.rootElt != null) {
            try {
                Element resourcesElt = launcher.rootElt.getChild(JNLP_TAG_ELT_RESOURCES);
                if (resourcesElt == null) {
                    throw new IllegalStateException("JNLP TAG : <" + JNLP_TAG_ELT_RESOURCES + "> is not found");
                }

                ConnectorProperties props =
                    (ConnectorProperties) this.getServletContext().getAttribute("componentProperties");
                Boolean allowEmptyMarker = Boolean.parseBoolean(props.getProperty("jnlp.allow.empty.marker"));

                filterMarkerInAttribute(resourcesElt.getChildren(), launcher.parameterMap, allowEmptyMarker);

                Element applicationElt = launcher.rootElt.getChild(JNLP_TAG_ELT_APPLICATION_DESC);
                if (applicationElt == null) {
                    throw new IllegalStateException("JNLP TAG : <application-desc> is not found");
                } else {
                    filterMarkerInElement(applicationElt.getChildren(JNLP_TAG_ELT_ARGUMENT), launcher.parameterMap,
                        allowEmptyMarker);
                }

            } catch (Exception e) {
                throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE,
                    "Can't modify URLProperty value in launcher template", e);
            }
        }
    }
 
Example 6
Source File: ArtifactCacheHandler.java    From buck with Apache License 2.0 5 votes vote down vote up
private int handlePut(Request baseRequest, HttpServletResponse response) throws IOException {
  Path temp = null;
  try {
    projectFilesystem.mkdirs(projectFilesystem.getBuckPaths().getScratchDir());
    temp =
        projectFilesystem.createTempFile(
            projectFilesystem.getBuckPaths().getScratchDir(), "incoming_upload", ".tmp");

    HttpArtifactCacheBinaryProtocol.StoreResponseReadResult storeRequest;
    try (DataInputStream requestInputData = new DataInputStream(baseRequest.getInputStream());
        OutputStream tempFileOutputStream = projectFilesystem.newFileOutputStream(temp)) {
      storeRequest =
          HttpArtifactCacheBinaryProtocol.readStoreRequest(
              requestInputData, tempFileOutputStream);
    }

    if (!storeRequest.getActualHashCode().equals(storeRequest.getExpectedHashCode())) {
      response.getWriter().write("Checksum mismatch.");
      return HttpServletResponse.SC_NOT_ACCEPTABLE;
    }

    artifactCache
        .get()
        .store(
            ArtifactInfo.builder()
                .setRuleKeys(storeRequest.getRuleKeys())
                .setMetadata(storeRequest.getMetadata())
                .build(),
            BorrowablePath.borrowablePath(temp));
    return HttpServletResponse.SC_ACCEPTED;
  } finally {
    if (temp != null) {
      projectFilesystem.deleteFileAtPathIfExists(temp);
    }
  }
}
 
Example 7
Source File: InvalidContentTypeException.java    From tus-java-server with MIT License 4 votes vote down vote up
public InvalidContentTypeException(String message) {
    super(HttpServletResponse.SC_NOT_ACCEPTABLE, message);
}
 
Example 8
Source File: JaxRsWebApplicationExceptionHandlerListener.java    From backstopper with Apache License 2.0 4 votes vote down vote up
@Override
public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) {

    ApiExceptionHandlerListenerResult result;
    SortedApiErrorSet handledErrors = null;
    List<Pair<String, String>> extraDetailsForLogging = new ArrayList<>();

    if (ex instanceof NotFoundException) {
        handledErrors = singletonSortedSetOf(projectApiErrors.getNotFoundApiError());
    }
    else if (ex instanceof WebApplicationException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        WebApplicationException webex = (WebApplicationException) ex;
        Response webExResponse = webex.getResponse();
        if (webExResponse != null) {
            int webExStatusCode = webExResponse.getStatus();
            if (webExStatusCode == HttpServletResponse.SC_NOT_ACCEPTABLE) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getNoAcceptableRepresentationApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getUnsupportedMediaTypeApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_METHOD_NOT_ALLOWED) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getMethodNotAllowedApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_UNAUTHORIZED) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getUnauthorizedApiError());
            }
        }
    }
    else if (ex instanceof JsonProcessingException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        handledErrors = singletonSortedSetOf(projectApiErrors.getMalformedRequestApiError());
    }

    // Return an indication that we will handle this exception if handledErrors got set
    if (handledErrors != null) {
        result = ApiExceptionHandlerListenerResult.handleResponse(handledErrors, extraDetailsForLogging);
    }
    else {
        result = ApiExceptionHandlerListenerResult.ignoreResponse();
    }

    return result;
}
 
Example 9
Source File: Jersey1WebApplicationExceptionHandlerListener.java    From backstopper with Apache License 2.0 4 votes vote down vote up
@Override
public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) {

    ApiExceptionHandlerListenerResult result;
    SortedApiErrorSet handledErrors = null;
    List<Pair<String, String>> extraDetailsForLogging = new ArrayList<>();

    if (ex instanceof NotFoundException) {
        handledErrors = singletonSortedSetOf(projectApiErrors.getNotFoundApiError());
    }
    else if (ex instanceof ParamException.URIParamException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        // Returning a 404 is intentional here.
        //      The Jersey contract for URIParamException states it should map to a 404.
        handledErrors = singletonSortedSetOf(projectApiErrors.getNotFoundApiError());
    }
    else if (ex instanceof ParamException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        handledErrors = singletonSortedSetOf(projectApiErrors.getMalformedRequestApiError());
    }
    else if (ex instanceof WebApplicationException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        WebApplicationException webex = (WebApplicationException) ex;
        Response webExResponse = webex.getResponse();
        if (webExResponse != null) {
            int webExStatusCode = webExResponse.getStatus();
            if (webExStatusCode == HttpServletResponse.SC_NOT_ACCEPTABLE) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getNoAcceptableRepresentationApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getUnsupportedMediaTypeApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_METHOD_NOT_ALLOWED) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getMethodNotAllowedApiError());
            }
            else if (webExStatusCode == HttpServletResponse.SC_UNAUTHORIZED) {
                handledErrors = singletonSortedSetOf(projectApiErrors.getUnauthorizedApiError());
            }
        }
    }
    else if (ex instanceof JsonProcessingException) {
        utils.addBaseExceptionMessageToExtraDetailsForLogging(ex, extraDetailsForLogging);
        handledErrors = singletonSortedSetOf(projectApiErrors.getMalformedRequestApiError());
    }

    // Return an indication that we will handle this exception if handledErrors got set
    if (handledErrors != null) {
        result = ApiExceptionHandlerListenerResult.handleResponse(handledErrors, extraDetailsForLogging);
    }
    else {
        result = ApiExceptionHandlerListenerResult.ignoreResponse();
    }

    return result;
}
 
Example 10
Source File: JnlpLauncher.java    From weasis-pacs-connector with Eclipse Public License 2.0 4 votes vote down vote up
protected void handleRequestPropertyParameter(JnlpTemplate launcher) throws ServletErrorException {
    String[] propValues = ServletUtil.getParameters(launcher.parameterMap.remove(WeasisConfig.PARAM_PROPERTY));

    if (launcher.rootElt != null && propValues != null) {
        try {
            Element resourcesElt = launcher.rootElt.getChild(JNLP_TAG_ELT_RESOURCES);

            if (resourcesElt == null) {
                throw new IllegalStateException("JNLP TAG : <" + JNLP_TAG_ELT_RESOURCES + "> is not found");
            }

            for (int i = 0; i < propValues.length; i++) {
                // split any whitespace character: [ \t\n\x0B\f\r ]
                String[] property = Pattern.compile("\\s").split(propValues[i], 2);

                String propertyName = property != null && property.length > 0 ? property[0] : null;
                String propertyValue = property != null && property.length > 1 ? property[1] : null;

                if (propertyName != null && propertyValue != null) {
                    boolean valueReplaced = false;

                    Iterator<Element> itr = resourcesElt.getChildren(JNLP_TAG_ELT_PROPERTY).iterator();
                    while (itr.hasNext()) {
                        Element elt = itr.next();
                        Attribute name = elt.getAttribute(JNLP_TAG_PRO_NAME);
                        if (name != null && name.getValue().equals(propertyName)) {
                            elt.setAttribute(JNLP_TAG_PRO_VALUE, propertyValue);
                            valueReplaced = true;
                            break;
                        }
                    }

                    if (!valueReplaced) {
                        Element propertyElt = new Element(JNLP_TAG_ELT_PROPERTY);
                        propertyElt.setAttribute(JNLP_TAG_PRO_NAME, propertyName);
                        propertyElt.setAttribute(JNLP_TAG_PRO_VALUE, propertyValue);
                        resourcesElt.addContent(propertyElt);
                    }
                } else {
                    throw new IllegalStateException(
                        "Query Parameter {property} is invalid : " + Arrays.toString(propValues));
                }
            }
        } catch (Exception e) {
            throw new ServletErrorException(HttpServletResponse.SC_NOT_ACCEPTABLE,
                "Cannot add property parameter to launcher template", e);
        }
    }
}