/*
@see javax.servlet.ServletRequest#getContentType()
5954
@Trivial
    @Override
    public String getContentType() {
        String type = this.request.getHeader("Content-Type");
        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, "getContentType: " + type);
        }
        return type;
    }
47606
public String getContentAsString() {
		if (contentAsString == null) {
			contentAsString = new String(rawContent,
					UriComponentEncoder.UTF8_CHARSET);
		}
		return contentAsString;
	}
331409
private MediaType findContentType(final String url) {
        if (url == null) {
            return null;
        }

        if (url.startsWith("file")) {
            return APPLICATION_OCTET_STREAM_TYPE;
        } else if (url.startsWith("http")) {
            try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
                final HttpHead httpHead = new HttpHead(url);
                try (CloseableHttpResponse response = httpClient.execute(httpHead)) {
                    if (response.getStatusLine().getStatusCode() == SC_OK) {
                        final Header contentType = response.getFirstHeader(CONTENT_TYPE);
                        if (contentType != null) {
                            return MediaType.valueOf(contentType.getValue());
                        }
                    }
                }
            } catch (final IOException e) {
                LOGGER.warn("Unable to retrieve external content from {} due to {}", url, e.getMessage());
            } catch (final Exception e) {
                throw new RepositoryRuntimeException(e);
            }
        }
        LOGGER.debug("Defaulting to octet stream for media type");
        return APPLICATION_OCTET_STREAM_TYPE;
    }
718142
public static String getContentType(String filename) {
		final ContentFileTypeMap map = ensureContentTypeManager();
		return map.getContentType(filename);
	}
856334
public function getContentType()
    {
        if (isset($this->responseInfo['content_type'])) {
            $contentType = $this->responseInfo['content_type'];
        } else {
            $contentType = isset($this->responseHeaders['Content-Type']) ? $this->responseHeaders['Content-Type'] : null;
        }
        
        if ($contentType) {
            $contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
            return strtolower($contentTypeParts[0]);
        }
        
        return null;
    }
1282874
@Override
    public RequestContent getRequestContent(HttpServletRequest request,
                                            HttpHeaders headers)
        throws Exception {
        RequestContent rContent = null;

        // See if the request is a multi-part file upload request
        if (ServletFileUpload.isMultipartContent(request)) {

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload();

            // Parse the request, use the first available File item
            FileItemIterator iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                if (!item.isFormField()) {
                    rContent = new RequestContent();
                    rContent.contentStream = item.openStream();
                    rContent.mimeType = item.getContentType();

                    FileItemHeaders itemHeaders = item.getHeaders();
                    if (itemHeaders != null) {
                        String contentLength =
                            itemHeaders.getHeader("Content-Length");
                        if (contentLength != null) {
                            rContent.size = Long.parseLong(contentLength);
                        }
                    }

                    break;
                }
            }
        } else {
            // If the content stream was not found as a multipart,
            // try to use the stream from the request directly
            rContent = new RequestContent();
            rContent.contentStream = request.getInputStream();
            if (request.getContentLength() >= 0) {
                rContent.size = request.getContentLength();
            }
        }

        // Attempt to set the mime type and size if not already set
        if (rContent != null) {
            if (rContent.mimeType == null) {
                MediaType mediaType = headers.getMediaType();
                if (mediaType != null) {
                    rContent.mimeType = mediaType.toString();
                }
            }

            if (rContent.size == 0) {
                List<String> lengthHeaders =
                    headers.getRequestHeader("Content-Length");
                if (lengthHeaders != null && lengthHeaders.size() > 0) {
                    rContent.size = Long.parseLong(lengthHeaders.get(0));
                }
            }
        }

        return rContent;
    }
1070390
public <T extends Serializable> T getContentFromType() throws FoxHttpResponseException {
        try {
            return getContent((Class<T>) Class.forName(this.type), false);
        } catch (FoxHttpResponseException e) {
            throw e;
        } catch (Exception e) {
            throw new FoxHttpResponseException(e);
        }

    }
324014
public void setContentType(String type) {
        final boolean isTraceOn = com.ibm.ejs.ras.TraceComponent.isAnyTracingEnabled();
        // 311717
        if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
            logger.entering(CLASS_NAME,"setContentType", " type --> " + type+ " ["+this+"]");
        }
        //PM50313
        if (type == null){
            if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
                logger.exiting(CLASS_NAME,"setContentType","not set - type is NULL");
            }  
            return;
        }
        //PM50313

        // d151464 - check the include flag
        WebAppDispatcherContext dispatchContext = (WebAppDispatcherContext) getRequest().getWebAppDispatcherContext();
        if (dispatchContext.isInclude() == true) {
            if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
                logger.exiting(CLASS_NAME,"setContentType", nls.getString("Illegal.from.included.servlet", "Illegal from included servlet"));  //311717
            }
            return;
        }


        if (isCommitted()){
            if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
                logger.exiting(CLASS_NAME,"setContentType","not set - response isCommitted");  //311717
            }
            return;
        }

        int equalsIndex = type.indexOf('=');
        boolean containsCharset = false;
        int beginCharsetStrIndex = equalsIndex - 7;
        int startCharsetValIndex = equalsIndex + 1;
        boolean isPreV7 = false;
        if (contentTypeCompatibility!=null && (contentTypeCompatibility.equalsIgnoreCase("V4")||
                        contentTypeCompatibility.equalsIgnoreCase("V5")||contentTypeCompatibility.equalsIgnoreCase("V6"))){
            isPreV7 = true;
        }

        boolean addEncoding = false;

        if (beginCharsetStrIndex > -1){
            containsCharset = type.regionMatches(true, beginCharsetStrIndex, "charset", 0, 7);
        }
        if (containsCharset){
            if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
                logger.logp(Level.FINE, CLASS_NAME,"setContentType", " isPreV7 --> " + isPreV7);
            }
            if (!_gotWriter) {  // 249506, part 2
                // Example:
                // setContentType("text/html;charset=UTF-8")
                // getWriter();
                // result = text/html;charset=UTF-8
                // we can change the character encoding because we haven't gotten the writer or committed
                // Begin 287829, Fix setContentType when qoutes are a part of the charset

                int endIndex = type.length() - 1;
                if (startCharsetValIndex<=endIndex){
                    isCharEncodingExplicit = true;
                    boolean startsWithQoute = type.charAt(startCharsetValIndex) == '"' || type.charAt(startCharsetValIndex) == '\'';
                    boolean endsWithQoute = type.charAt(endIndex) == '"' || type.charAt(endIndex) == '\'';
                    if (startsWithQoute && endsWithQoute){
                        _encoding = type.substring(startCharsetValIndex + 1, endIndex);
                        addEncoding = true;
                        // remove the qoutes from the encoding and add later
                    }
                    else {
                        _encoding = type.substring(startCharsetValIndex);
                        // type is perfectly formed and can be set as is.
                    }

                }
                // End 287829, Fix setContentType when qoutes are a part of the charset
            }
            else {
                if (isPreV7){
                    // Before v7, strip the charset
                    // Example:
                    // setContentType("text/html");
                    // getWriter();
                    // setContentType("text/xml;charset=UTF-8");
                    // result = text/xml
                    if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
                        logger.logp(Level.FINE, CLASS_NAME,"setContentType", " isPreV7, strip charset ");
                    }
                    type = type.substring(0, beginCharsetStrIndex).trim();
                    // if (type.endsWith(";")) { //wasteful??? It has to end with ;
                    type = type.substring(0, type.length() - 1);
                    // }
                }
            }
        }
        if (!isPreV7){
            if (_gotWriter){
                //we've gotten the writer, but not committed, so we can change the mime type,
                //but not the charset. Still we have to add the default encoding or the previous
                // charset
                // setContentType("text/html;charset=ISO-8859-7");
                // getWriter();
                // setContentType("text/xml;charset=UTF-8");
                // result = text/xml;charset=ISO-8859-7
                if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
                    logger.logp(Level.FINE, CLASS_NAME,"setContentType", " add encoding because we retrieved a writer");
                }
                addEncoding = true;
            }
            else {
                //we haven't gotten the writer, and the current contentType argument has no
                //charset included. Therefore check to see if the old content type has a charset
                // and append it *** if the new type is some form of text ***
                // setContentType("text/html;charset=UTF-8");
                // setContentType("text/xml");
                // result = text/xml;charset=UTF-8
                if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
                    logger.logp(Level.FINE, CLASS_NAME,"setContentType", " did not get writer, check to see if charset already exists");
                }
                if (isCharEncodingExplicit&&type.startsWith(WebContainerConstants.TEXT)){ //506208.1
                    if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
                        logger.logp(Level.FINE, CLASS_NAME,"setContentType", " add encoding because the content type already had a charset");
                    }
                    addEncoding = true;
                }
            }
        }

        if (addEncoding)
        {
            if (containsCharset){
                type = type.substring(0, startCharsetValIndex) + _encoding;
            }
            else
            {
                type = type + ";" + WebContainerConstants.CHARSET_EQUALS + _encoding;
            }
        }
        /* 252465: We do not want to do the autoResponseEncoding check here
         * because this sets the charset for binary data also.
         * Only use in the case of getWriter.
         */
        _contentType = type;
        if (_response != null)
            _response.setContentType(type);

        if (isTraceOn&&logger.isLoggable (Level.FINE)) {  //306998.15
            logger.exiting(CLASS_NAME,"setContentType","type="+_contentType);  //311717
        }

    }
1005630
static String getCharsetFromContentType(String contentType) {
        if (contentType == null) return null;
        Matcher m = charsetPattern.matcher(contentType);
        if (m.find()) {
            String charset = m.group(1).trim();
            charset = charset.replace("charset=", "");
            return validateCharset(charset);
        }
        return null;
    }
1275122
@Override
    public String getCharsetFromContentType(String type) {
        if (null != type) {
            int index = type.indexOf(';');
            if (-1 != index) {
                index = type.indexOf("charset=", (index + 1));
                if (-1 != index) {
                    String charset = type.substring(index + 8).trim();
                    int end = charset.length() - 1;
                    if (-1 == end) {
                        // empty value following charset=
                        return null;
                    }
                    if (end > 0 && '"' == charset.charAt(0) && '"' == charset.charAt(end)) {
                        // strip off quotes if both exist
                        charset = charset.substring(1, end);
                    }
                    return charset;
                }
            }
        }
        return null;
    }
188121
@Override
    public String getContent() {
        FileReader reader = null;
        try {
            reader = new FileReader(this.contentFile);
            return IOUtils.toString(reader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(reader);
        }
    }
537132
public function getContentType(): string
    {
        // If the response hasn't been prepared yet, go with what the formatter is going to set
        if (!$this->_isPrepared) {
            switch ($this->format) {
                case self::FORMAT_HTML:
                    return 'text/html';
                case self::FORMAT_XML:
                    return 'application/xml';
                case self::FORMAT_JSON:
                    return 'application/json';
                case self::FORMAT_JSONP:
                    return 'application/javascript';
            }
        }

        // Otherwise check the Content-Type header
        if (($header = $this->getHeaders()->get('content-type')) === null) {
            return null;
        }

        if (($pos = strpos($header, ';')) !== false) {
            $header = substr($header, 0, $pos);
        }

        return strtolower(trim($header));
    }
414913
protected String getContentType(Resource resource, @SuppressWarnings("unused") SlingHttpServletRequest request) {
    String mimeType = JcrBinary.getMimeType(resource);
    if (StringUtils.isEmpty(mimeType)) {
      mimeType = ContentType.OCTET_STREAM;
    }
    return mimeType;
  }
139525
public static byte getSerializeTypeByContentType(String contentType) throws SofaRpcException {
        if (StringUtils.isNotBlank(contentType)) {
            String ct = contentType.toLowerCase();
            if (ct.contains("text/plain") || ct.contains("text/html") || ct.contains("application/json")) {
                return getSerializeTypeByName(RpcConstants.SERIALIZE_JSON);
            } else if (ct.contains(RpcConstants.SERIALIZE_PROTOBUF)) {
                return getSerializeTypeByName(RpcConstants.SERIALIZE_PROTOBUF);
            } else if (ct.contains(RpcConstants.SERIALIZE_HESSIAN)) {
                return getSerializeTypeByName(RpcConstants.SERIALIZE_HESSIAN2);
            }
        }
        throw new SofaRpcException(RpcErrorType.SERVER_DESERIALIZE, "Unsupported content type " + contentType
            + " in http protocol, please set HTTP HEAD: '" + RemotingConstants.HEAD_SERIALIZE_TYPE + "'.");
    }
1052471
private static String getContentTypeMime(final String httpContentType) {
        String mime = null;
        if (httpContentType != null) {
            final int i = httpContentType.indexOf(";");
            if (i == -1) {
                mime = httpContentType.trim();
            } else {
                mime = httpContentType.substring(0, i).trim();
            }
        }
        return mime;
    }
1035289
function getContentType(url: string): string {
  url = trimUrlQuery(url);
  const dot = url.lastIndexOf(".");
  if (dot >= 0) {
    const name = url.substring(dot + 1);
    if (name in contentMap) {
      return contentMap[name];
    }
  }
  return "text/plain";
}
1062343
public Object getContent() throws IOException, UnsupportedEncodingException {
		ContentTypeHeader contentTypeHeader = (ContentTypeHeader)
 			this.message.getHeader(ContentTypeHeader.NAME);
		if(contentTypeHeader != null && logger.isDebugEnabled()) {
			logger.debug("Content type " + contentTypeHeader.getContentType());
			logger.debug("Content sub type " + contentTypeHeader.getContentSubType());
		}
		if(contentTypeHeader!= null && CONTENT_TYPE_TEXT.equals(contentTypeHeader.getContentType())) {
			String content = null;
			if(message.getRawContent() != null) {
				String charset = this.getCharacterEncoding();
				if(charset == null) {
					content = new String(message.getRawContent());
				} else {
					content = new String(message.getRawContent(), charset);
				}
			} else {
				content = "";
			}
			return content;
		} else if(contentTypeHeader!= null && CONTENT_TYPE_MULTIPART.equals(contentTypeHeader.getContentType())) {
			try {
				return new MimeMultipart(new ByteArrayDataSource(message.getRawContent(),
						contentTypeHeader.toString().replaceAll(ContentTypeHeader.NAME+": ", "")));
			} catch (MessagingException e) {
				logger.warn("Problem with multipart message.", e);
				return this.message.getRawContent();
			}
		} else {
			return this.message.getRawContent();
		}
	}