Retrieves external content via http or https.

@return A MIMETypedStream
@throws ModuleInitializationException
@throws GeneralException
@throws RangeNotSatisfiableException
347028
private MIMETypedStream getFromWeb(ContentManagerParams params)
            throws ModuleInitializationException, GeneralException, RangeNotSatisfiableException {
           String username = params.getUsername();
        String password = params.getPassword();
        boolean backendSSL = false;
        String url = params.getUrl();
        // in case host is 'local.fedora.server', and has not been normalized (e.g. on validating datastream add)
        url = params.getTranslator().makeAbsoluteURLs(url);
        if (ServerUtility.isURLFedoraServer(url) && !params.isBypassBackend()) {
            BackendSecuritySpec m_beSS;
            BackendSecurity m_beSecurity =
                    (BackendSecurity) getServer()
                            .getModule("org.fcrepo.server.security.BackendSecurity");
            try {
                m_beSS = m_beSecurity.getBackendSecuritySpec();
            } catch (Exception e) {
                throw new ModuleInitializationException(
                        "Can't intitialize BackendSecurity module (in default access) from Server.getModule",
                        getRole());
            }
            Hashtable<String, String> beHash =
                    m_beSS.getSecuritySpec(BackendPolicies.FEDORA_INTERNAL_CALL);
            username = beHash.get("callUsername");
            password = beHash.get("callPassword");
            backendSSL =
                    Boolean.parseBoolean(beHash.get("callSSL"));
            if (backendSSL) {
                if (params.getProtocol().equals("http")) {
                    url = url.replaceFirst("http:", "https:");
                }
                url =
                        url.replaceFirst(":" + fedoraServerPort + "/",
                                            ":" + fedoraServerRedirectPort
                                                    + "/");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("************************* backendUsername: "
                        + username + "     backendPassword: "
                        + password + "     backendSSL: " + backendSSL);
                logger.debug("************************* doAuthnGetURL: " + url);
            }

        }
        return getFromWeb(url, username, password, params.getMimeType(),
                isHEADRequest(params), params.getContext());
    }
763282
public function getMimeType()
    {
        $mimetypeRepo = new PhpRepository();
        $path = $this->getPathFilename();

        return $mimetypeRepo->findType(pathinfo($path, PATHINFO_EXTENSION)) ?: 'application/octet-stream';
    }
1175736
public InputStream getInputStream(URI uri, SecurityDomain securityDomain) throws NoSecurityDomainException, MalformedURLException, IOException, NoSuchAlgorithmException {
        final NodeAuthModuleContext context = NodeAuthModuleContext.getContext();
        if (!context.isTLSEnabled() && !context.isNonTLSConnectionsPermitted()) {
            throw new NoSuchAlgorithmException("TLS has been disabled for ATNA connections");
        }

        boolean tlsURI = uri.getScheme().equalsIgnoreCase("https");
        boolean useTLS = tlsURI && context.isTLSEnabled() || !context.isNonTLSConnectionsPermitted();

        if (useTLS && null == securityDomain) {
            throw new NoSecurityDomainException(uri, "Security domain provided is null");
        }

        if (useTLS) {
            securityDomain.setDomainEnvironment();
        }
        InputStream is = null;
        try {
            URLConnection connection = uri.toURL().openConnection();

            if (useTLS && !securityDomain.doDomainSpoofCheck()) {
                ((HttpsURLConnection) connection).setHostnameVerifier((urlHostName, session) -> true);
            }

            is = connection.getInputStream();
            if (useTLS) {
                logger.info("Secure connection successfully made using TLS to " + uri.toString());
            } else {
                logger.info("Unsecure connection successfully made to " + uri.toString());
            }
        } catch (IOException e) {
            if (useTLS) securityDomain.restoreSystemEnvironment();
            throw e;
        }

        if (useTLS) securityDomain.restoreSystemEnvironment();
        return is;
    }
313565
public InputStream getContentReplayInputStream() throws IOException {
        InputStream entityStream = getEntityReplayInputStream();
        if(StringUtils.isEmpty(contentEncoding)) {
            return entityStream;
        } else if ("gzip".equalsIgnoreCase(contentEncoding) || "x-gzip".equalsIgnoreCase(contentEncoding)) {
            try {
                return new GZIPInputStream(entityStream);
            } catch (IOException ioe) {
                logger.log(Level.WARNING,"gzip problem; using raw entity instead",ioe);
                IOUtils.closeQuietly(entityStream); // close partially-read stream
                return getEntityReplayInputStream(); 
            }
        } else if ("deflate".equalsIgnoreCase(contentEncoding)) {
            return new DeflaterInputStream(entityStream);
        } else if ("identity".equalsIgnoreCase(contentEncoding) || "none".equalsIgnoreCase(contentEncoding)) {
            return entityStream;
        } else {
            // shouldn't be reached given check on setContentEncoding
            logger.log(Level.INFO,"Unknown content-encoding '"+contentEncoding+"' declared; using raw entity instead");
            return entityStream; 
        }
    }
806019
@Override
    public InputStream getAsStream() throws BlogClientException {
        final HttpClient httpClient = new HttpClient();
        final GetMethod method = new GetMethod(permalink);
        try {
            httpClient.executeMethod(method);
        } catch (final Exception e) {
            throw new BlogClientException("ERROR: error reading file", e);
        }
        if (method.getStatusCode() != 200) {
            throw new BlogClientException("ERROR HTTP status=" + method.getStatusCode());
        }
        try {
            return method.getResponseBodyAsStream();
        } catch (final Exception e) {
            throw new BlogClientException("ERROR: error reading file", e);
        }
    }
1369668
@GET
    public Response getStreamContents() {

        // Intercept all output
        StreamingOutput stream = new StreamingOutput() {

            @Override
            public void write(OutputStream output) throws IOException {
                try {
                    tunnel.interceptStream(streamIndex, output);
                }
                catch (GuacamoleException e) {
                    throw new IOException(e);
                }
            }

        };

        // Begin successful response
        ResponseBuilder responseBuilder = Response.ok(stream, mediaType);

        // Set Content-Disposition header for "application/octet-stream"
        if (mediaType.equals(MediaType.APPLICATION_OCTET_STREAM))
            responseBuilder.header("Content-Disposition", "attachment");

        return responseBuilder.build();

    }
1129980
public InputStream getInputStream() throws APIException, IOException {
        final HttpResponse response = client.get(getUrl());
        HttpClientHelpers.throwIfNotOk(response);
        return response.getEntity().getContent();
    }
155344
public static InputStream getRequestInputStream(ExternalContext ec)
            throws IOException
    {
        RequestType type = getRequestType(ec);
        if (type.isRequestFromClient())
        {
            Object req = ec.getRequest();
            if (type.isPortlet())
            {
                return (InputStream) _runMethod(req, "getPortletInputStream");
            }
            else
            {
                return ((ServletRequest) ec.getRequest()).getInputStream();
            }
        }

        return null;
    }
1080911
@Route(method = HttpMethod.GET, uri = "/manifest-as-stream")
    public Result manifestAsStream() throws MalformedURLException {
        InputStream is = this.getClass()
                .getClassLoader().getResourceAsStream("/META-INF/MANIFEST.MF");
        return ok(is).as(MimeTypes.TEXT);
    }
1160310
@Override public final InputStream getErrorStream() {
    try {
      HttpEngine response = getResponse();
      if (HttpEngine.hasBody(response.getResponse())
          && response.getResponse().code() >= HTTP_BAD_REQUEST) {
        return response.getResponse().body().byteStream();
      }
      return null;
    } catch (IOException e) {
      return null;
    }
  }
197983
public static HttpServletRequest getHttpServletRequestFromExternalWebflowContext() {
        val servletExternalContext = (ServletExternalContext) ExternalContextHolder.getExternalContext();
        if (servletExternalContext != null) {
            return (HttpServletRequest) servletExternalContext.getNativeRequest();
        }
        return null;

    }
156833
public synchronized InputStream getInputStream() throws IOException {
        if (!connected) {
            connect();
        }

        // Nothing to return
        if (responseCode == HTTP_NOT_FOUND) {
            throw new FileNotFoundException(url.toString());
        }
        int length;

        if (inputStream == null) {
            return null;
        }

        // "De-chunk" the output stream
        else if ("chunked".equalsIgnoreCase(getHeaderField("Transfer-Encoding"))) {
            if (!(inputStream instanceof ChunkedInputStream)) {
                inputStream = new ChunkedInputStream(inputStream);
            }
        }

        // Make sure we don't wait forever, if the content-length is known
        else if ((length = getHeaderFieldInt("Content-Length", -1)) >= 0) {
            if (!(inputStream instanceof FixedLengthInputStream)) {
                inputStream = new FixedLengthInputStream(inputStream, length);
            }
        }
        return inputStream;
    }
301617
private MIMETypedStream getFromFilesystem(ContentManagerParams params)
            throws HttpServiceNotFoundException,RangeNotSatisfiableException,GeneralException {
        logger.debug("in getFromFilesystem(), url={}", params.getUrl());

        try {
            URL fileUrl = new URL(params.getUrl());
            File cFile = new File(fileUrl.toURI()).getCanonicalFile();
            // security check
            URI cURI = cFile.toURI();
            logger.info("Checking resolution security on {}", cURI);
            Authorization authModule = getServer()
                    .getBean("org.fcrepo.server.security.Authorization", Authorization.class);
            if (authModule == null) {
                throw new GeneralException(
                "Missing required Authorization module");
            }
            String cUriString = cURI.toString();
            authModule.enforceRetrieveFile(params.getContext(), cUriString);
            // end security check
            String mimeType = params.getMimeType();

            // if mimeType was not given, try to determine it automatically
            if (mimeType == null || mimeType.equalsIgnoreCase("")){
                mimeType = determineMimeType(cFile);
            }
            Property [] headers = getFileDatastreamHeaders(cUriString, cFile.lastModified());
            if (ServerUtility.isStaleCache(params.getContext(), headers)) {
                MIMETypedStream result;
                if (isHEADRequest(params)) {
                    result = new MIMETypedStream(mimeType, NullInputStream.NULL_STREAM,
                            headers, cFile.length());
                } else {
                    result = new MIMETypedStream(mimeType, fileUrl.openStream(),
                            headers, cFile.length());
                }
                String rangeHdr =
                        params.getContext().getHeaderValue(HttpHeaders.RANGE);
                if (rangeHdr != null) {
                    result.setRange(rangeHdr);;
                }
                return result;
                
            } else {
                return MIMETypedStream.getNotModified(headers);
            }
        }
        catch(AuthzException ae){
            logger.error(ae.getMessage(),ae);
            throw new HttpServiceNotFoundException("Policy blocked datastream resolution",ae);
        }
        catch (RangeNotSatisfiableException re){
        	logger.error(re.getMessage(), re);
        	throw re;
        }
        catch (GeneralException me) {
            logger.error(me.getMessage(),me);
            throw me;
        } catch (Throwable th) {
            th.printStackTrace(System.err);
            // catch anything but generalexception
            logger.error(th.getMessage(),th);
             throw new HttpServiceNotFoundException("[FileExternalContentManager] "
                    + "returned an error.  The underlying error was a "
                    + th.getClass().getName()
                    + "  The message "
                    + "was  \""
                    + th.getMessage() + "\"  .  ",th);
        }
    }
1247284
@SuppressWarnings("unchecked")
	public T get() {
		T result;

		if (uri != null) {
			if (classLoader == null) {
				classLoader = clazz.getClassLoader();
			}

			Endpoint clientEndpoint = new HTTPClientEndpoint(uri);
			if (logger != null) {
				clientEndpoint = new LoggerEndpoint(clientEndpoint, logger);
			}

			result = new FacadeEndpoint<T>(clientEndpoint, //
					classLoader, clazz, typeConverter, //
					additionalInterfaces.toArray(new Class<?>[additionalInterfaces.size()])) //
					.newProxy();
		} else {
			ObjectEndpoint endpoint = new ObjectEndpoint();
			endpoint.faultMapper(faultMapper);
			endpoint.typeConverter(typeConverter);
			endpoint.export(server, clazz);
			for (Class<?> i : additionalInterfaces) {
				endpoint.export(server, i);
			}
			result = (T) new HTTPServerEndpoint(endpoint);
		}

		return result;
	}
184382
@Override
  public StreamImpl stream()
    throws IOException
  {
    if (_channelStream == null) {
      _channelStream = new SocketChannelStream();
    }
    
    if (_sslSocket == null) {
      _channelStream.init(_channel);
      _streamImpl = _channelStream;
    }
    else {
      if (_sslStream == null) {
        _sslStream = new SocketStream();
      }
      
      _sslStream.init(_sslSocket);
      _streamImpl = _sslStream;
    }

    return _streamImpl;
  }
324700
public String getContentType() {
        String contentType = getHeader(HttpConstants.Header.CONTENT_TYPE);
        if (contentType == null) {
            contentType = httpServletResponse.getContentType();
        }

        return contentType;
    }
606453
public static Function<Service<HttpRequest, HttpResponse>,
            HttpEncodingService> contentEncodingDecorator(@Nullable String[] mimeTypes,
                                                          @Nullable String[] excludedUserAgents,
                                                          long minBytesToForceChunkedAndEncoding) {
        final Predicate<MediaType> encodableContentTypePredicate;
        if (mimeTypes == null || mimeTypes.length == 0) {
            encodableContentTypePredicate = contentType -> true;
        } else {
            final List<MediaType> encodableContentTypes =
                    Arrays.stream(mimeTypes).map(MediaType::parse).collect(toImmutableList());
            encodableContentTypePredicate = contentType ->
                    encodableContentTypes.stream().anyMatch(contentType::is);
        }

        final Predicate<HttpHeaders> encodableRequestHeadersPredicate;
        if (excludedUserAgents == null || excludedUserAgents.length == 0) {
            encodableRequestHeadersPredicate = headers -> true;
        } else {
            final List<Pattern> patterns =
                    Arrays.stream(excludedUserAgents).map(Pattern::compile).collect(toImmutableList());
            encodableRequestHeadersPredicate = headers -> {
                // No User-Agent header will be converted to an empty string.
                final String userAgent = headers.get(HttpHeaderNames.USER_AGENT, "");
                return patterns.stream().noneMatch(pattern -> pattern.matcher(userAgent).matches());
            };
        }

        return delegate -> new HttpEncodingService(delegate,
                                                   encodableContentTypePredicate,
                                                   encodableRequestHeadersPredicate,
                                                   minBytesToForceChunkedAndEncoding);
    }