/** * Gets the content type. * * @param uri the uri * @return the content type */ public static String getResponseBodyContentType(final HttpServletRequest request) { LOG.trace("Start VtnServiceCommonUtil#getContentType()"); String contentType = null; final String requestURI = request.getRequestURI(); if (requestURI.lastIndexOf(ApplicationConstants.DOT_REGEX) != -1) { String extension = requestURI.substring(requestURI.lastIndexOf(ApplicationConstants.DOT_REGEX)); if (extension.indexOf(ApplicationConstants.QUESTION_MARK_CHAR) != -1) { extension = extension.substring(0, extension.indexOf("?")); } if (null != extension && (extension.equalsIgnoreCase(ApplicationConstants.TYPE_XML) || extension.equalsIgnoreCase(ApplicationConstants.TYPE_JSON))) { if (extension.equalsIgnoreCase(ApplicationConstants.TYPE_XML)) { contentType = ContentTypeEnum.APPLICATION_XML.getContentType(); } else if (extension.equalsIgnoreCase(ApplicationConstants.TYPE_JSON)) { contentType = ContentTypeEnum.APPLICATION_JSON.getContentType(); } } } if (null == contentType) { final String acceptHeaderValue = request.getHeader(ApplicationConstants.HTTP_HEADER_ACCEPT); if (acceptHeaderValue != null) { String acceptHeader[] = acceptHeaderValue.split(ApplicationConstants.SEMI_COLON); if (acceptHeader.length > 0) { if (acceptHeader[0].equalsIgnoreCase(ContentTypeEnum.APPLICATION_JSON.getContentType()) || acceptHeader[0].equalsIgnoreCase( ContentTypeEnum.APPLICATION_XML.getContentType())) { contentType = acceptHeader[0]; } } } } if (null == contentType) { contentType = ContentTypeEnum.APPLICATION_JSON.getContentType(); } LOG.trace("Complete VtnServiceCommonUtil#getContentType()"); return contentType; }
/** * Validate uri. * * @param uri the uri * @param contentType the content type * @return true, if successful */ public static boolean validateMediaType(final HttpServletRequest request) { LOG.trace("Start VtnServiceCommonUtil#validateMediaType()"); boolean isMediaTypeCorrect = true; if (request.getMethod().equalsIgnoreCase(ApplicationConstants.POST_METHOD_NAME) || request.getMethod().equalsIgnoreCase(ApplicationConstants.PUT_METHOD_NAME)) { isMediaTypeCorrect = false; String contentType = request.getContentType(); LOG.debug("Media Type : " + contentType); if (contentType != null) { String content[] = contentType.split(ApplicationConstants.SEMI_COLON); if (content.length > 0 && (content[0].equalsIgnoreCase(ContentTypeEnum.APPLICATION_JSON.getContentType()) || content[0].equalsIgnoreCase(ContentTypeEnum.APPLICATION_XML.getContentType()) || content[0].equalsIgnoreCase( ContentTypeEnum.APPLICATION_XML_SCVMM.getContentType()))) { isMediaTypeCorrect = true; } } } LOG.debug("Valid Media Type : " + isMediaTypeCorrect); LOG.trace("Complete VtnServiceCommonUtil#validateMediaType()"); return isMediaTypeCorrect; }