Example usage for java.net URI toASCIIString

List of usage examples for java.net URI toASCIIString

Introduction

In this page you can find the example usage for java.net URI toASCIIString.

Prototype

public String toASCIIString() 

Source Link

Document

Returns the content of this URI as a US-ASCII string.

Usage

From source file:cz.cas.lib.proarc.common.export.mets.structure.MetsElementVisitor.java

/**
 * Prepares a mets FileType element for a file
 *
 * @param seq/*  ww  w  . j ava2 s.  c o  m*/
 * @param metsStreamName
 * @return
 */
private FileType prepareFileType(int seq, String metsStreamName, HashMap<String, Object> fileNames,
        HashMap<String, String> mimeTypes, MetsContext metsContext, HashMap<String, String> outputFileNames,
        HashMap<String, FileMD5Info> md5InfosMap) throws MetsExportException {
    // String streamName = Const.streamMapping.get(metsStreamName);
    FileType fileType = new FileType();
    fileType.setCHECKSUMTYPE("MD5");
    GregorianCalendar gregory = new GregorianCalendar();
    gregory.setTime(new Date());

    XMLGregorianCalendar calendar;
    try {
        calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregory);
    } catch (DatatypeConfigurationException e1) {
        throw new MetsExportException("Unable to create XMLGregorianDate", false, e1);
    }
    fileType.setCREATED(calendar);
    fileType.setSEQ(seq);
    fileType.setMIMETYPE(mimeTypes.get(metsStreamName));
    InputStream is = null;
    fileType.setID(Const.streamMappingPrefix.get(metsStreamName) + "_"
            + MetsUtils.removeNonAlpabetChars(metsContext.getPackageID()) + "_" + String.format("%04d", seq));
    if (fileNames.get(metsStreamName) instanceof String) {
        String fileNameOriginal = (String) fileNames.get(metsStreamName);
        int lastIndex = fileNameOriginal.lastIndexOf('/');
        int preLastIndex = fileNameOriginal.substring(1, lastIndex).lastIndexOf('/');
        String fileName = metsContext.getPath() + fileNameOriginal.substring(preLastIndex + 2);
        File file = new File(fileName);
        try {
            is = new FileInputStream(file);

        } catch (FileNotFoundException e) {
            throw new MetsExportException("File not found:" + fileName, false, e);
        }
    }
    if (fileNames.get(metsStreamName) instanceof byte[]) {
        byte[] bytes = (byte[]) fileNames.get(metsStreamName);
        is = new ByteArrayInputStream(bytes);
    }
    if (fileNames.get(metsStreamName) instanceof InputStream) {
        is = (InputStream) fileNames.get(metsStreamName);
    }

    if (metsStreamName.equalsIgnoreCase("TECHMDGRP")) {
        is = addLabelToAmdSec(is, metsContext);
    }

    String outputFileName = fileType.getID() + "." + MimeType.getExtension(mimeTypes.get(metsStreamName));
    String fullOutputFileName = metsContext.getPackageDir().getAbsolutePath() + File.separator
            + Const.streamMappingFile.get(metsStreamName) + File.separator + outputFileName;
    outputFileNames.put(metsStreamName, fullOutputFileName);
    try {
        FileMD5Info fileMD5Info;
        if (md5InfosMap.get(metsStreamName) == null) {
            fileMD5Info = MetsUtils.getDigestAndCopy(is, new FileOutputStream(fullOutputFileName));
            md5InfosMap.put(metsStreamName, fileMD5Info);
        } else {
            FileMD5Info tempMd5 = MetsUtils.getDigestAndCopy(is, new FileOutputStream(fullOutputFileName));
            fileMD5Info = md5InfosMap.get(metsStreamName);
            fileMD5Info.setSize(tempMd5.getSize());
            fileMD5Info.setMd5(tempMd5.getMd5());
        }
        fileType.setSIZE(Long.valueOf(fileMD5Info.getSize()));
        fileMD5Info.setFileName("." + File.separator + Const.streamMappingFile.get(metsStreamName)
                + File.separator + outputFileName);
        fileMD5Info.setMimeType(fileType.getMIMETYPE());
        fileType.setCHECKSUM(fileMD5Info.getMd5());
        metsContext.getFileList().add(fileMD5Info);
    } catch (Exception e) {
        throw new MetsExportException("Unable to process file " + fullOutputFileName, false, e);
    }
    FLocat flocat = new FLocat();
    flocat.setLOCTYPE("URL");
    String href = "." + "/" + Const.streamMappingFile.get(metsStreamName) + "/" + outputFileName;
    URI uri;
    uri = URI.create(href);
    flocat.setHref(uri.toASCIIString());
    fileType.getFLocat().add(flocat);
    return fileType;
}

From source file:uk.ac.open.kmi.iserve.sal.manager.impl.RegistryManagerImpl.java

/**
 * Imports a new service within iServe. The original document is stored
 * in the server and the transformed version registered within iServe.
 *
 * @param servicesContentStream//from  w ww.  j  a  v  a  2  s. c  om
 * @param mediaType
 * @return the List of URIs of the services imported
 * @throws SalException
 */
@Override
public List<URI> importServices(InputStream servicesContentStream, String mediaType) throws SalException {

    boolean isNativeFormat = MediaType.NATIVE_MEDIATYPE_SYNTAX_MAP.containsKey(mediaType);
    // Throw error if Format Unsupported
    if (!isNativeFormat && !this.serviceTransformationEngine.canTransform(mediaType)) {
        log.error("The media type {} is not natively supported and has no suitable transformer.", mediaType);
        throw new ServiceException("Unable to import service. Format unsupported.");
    }

    // Obtain the file extension to use
    String fileExtension = findFileExtensionToUse(mediaType, isNativeFormat);

    List<Service> services = null;
    List<URI> importedServices = new ArrayList<URI>();
    URI sourceDocUri = null;
    InputStream localStream = null;
    try {
        // 1st Store the document
        sourceDocUri = this.docManager.createDocument(servicesContentStream, fileExtension, mediaType);
        if (sourceDocUri == null) {
            throw new ServiceException("Unable to save service document. Operation aborted.");
        }

        // 2nd Parse and Transform the document
        // The original stream may be a one-of stream so save it first and read locally
        localStream = this.docManager.getDocument(sourceDocUri);
        services = getServicesFromStream(mediaType, isNativeFormat, localStream);

        // 3rd - Store the resulting MSM services. There may be more than one
        URI serviceUri = null;
        if (services != null && !services.isEmpty()) {
            log.info("Importing {} services", services.size());
            for (Service service : services) {
                // The service is being imported -> update the source
                service.setSource(sourceDocUri);
                serviceUri = this.serviceManager.addService(service);
                if (serviceUri != null) {
                    importedServices.add(serviceUri);
                }
            }
        }

        // 4th Log it was all done correctly
        // TODO: log to the system and notify observers
        log.info("Source document imported: {}", sourceDocUri.toASCIIString());

    } finally {
        // Rollback if something went wrong
        if ((services == null || (services != null && services.size() != importedServices.size()))
                && sourceDocUri != null) {
            this.docManager.deleteDocument(sourceDocUri);
            for (URI svcUri : importedServices) {
                this.serviceManager.deleteService(svcUri);
            }
            log.warn("There were problems importing the service. Changes undone.");
        }

        if (localStream != null)
            try {
                localStream.close();
            } catch (IOException e) {
                log.error("Error closing the service content stream", e);
            }
    }

    return importedServices;
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.rmapp.RMAppImpl.java

private String getDefaultProxyTrackingUrl() {
    try {/*from w  w w  .  j  a v a 2  s .  c  o  m*/
        final String scheme = WebAppUtils.getHttpSchemePrefix(conf);
        String proxy = WebAppUtils.getProxyHostAndPort(conf);
        URI proxyUri = ProxyUriUtils.getUriFromAMUrl(scheme, proxy);
        URI result = ProxyUriUtils.getProxyUri(null, proxyUri, applicationId);
        return result.toASCIIString();
    } catch (URISyntaxException e) {
        LOG.warn("Could not generate default proxy tracking URL for " + applicationId);
        return UNAVAILABLE;
    }
}

From source file:org.globus.workspace.service.impls.InstanceResourceImpl.java

private void handlePostTasks(ShutdownTasks tasks, boolean isReadyForTransport) throws ManageException {

    if (tasks == null) {
        return; // *** EARLY RETURN ***
    }// w w w  .j  a  v a 2 s  .  c om

    URI target = tasks.getBaseFileUnpropagationTarget();
    if (target == null) {
        return; // *** EARLY RETURN ***
    }

    // technically possible but only someone else's client implementation
    // would allow this to happen
    if (!isReadyForTransport) {
        final String err = "Post-shutdown tasks are only compatible " + "with a ReadyForTransport request.";
        throw new ManageException(err);
    }

    if (this.authzCallout != null && this.authzCallout instanceof PostTaskAuthorization) {

        // shortcut: currently we know owner is the caller at this point
        final String dnToAuthorize = this.creatorID;

        final Integer decision;
        String newTargetName;
        try {
            decision = ((PostTaskAuthorization) this.authzCallout).isRootPartitionUnpropTargetPermitted(target,
                    dnToAuthorize);
        } catch (AuthorizationException e) {
            throw new ManageException(e.getMessage(), e);
        }

        if (!Decision.PERMIT.equals(decision)) {
            throw new ManageException("request denied, no message for client");
        }
    }

    final String trueTarget;
    if (tasks.isAppendID()) {

        trueTarget = target.toASCIIString() + "-" + this.id;

        // check new uri syntax:
        try {
            new URI(trueTarget);
        } catch (URISyntaxException e) {
            throw new ManageException(e.getMessage(), e);
        }

    } else {

        trueTarget = target.toASCIIString();
    }

    this.vm.overrideRootUnpropTarget(trueTarget, logger);

    if (!this.isUnPropagateRequired()) {
        this.vm.setUnPropagateRequired(true);
    }

    this.persistence.setRootUnpropTarget(this.id, trueTarget);
}

From source file:org.ardverk.daap.DaapRequest.java

/**
 * Sets and parses the URI. Note: if URIException is thrown then is this
 * Request in an inconsistent state!//from  w  w w .  j av  a  2  s  . c om
 * 
 * @param uri
 * @throws URIException
 */
private void setURI(URI uri) throws URISyntaxException {

    this.uri = uri;

    if (uri != null) {

        String path = uri.getPath();

        this.queryMap = DaapUtil.parseQuery(uri.getQuery());

        if (path.equals("/server-info")) {
            requestType = SERVER_INFO;
        } else if (path.equals("/content-codes")) {
            requestType = CONTENT_CODES;
        } else if (path.equals("/login")) {
            requestType = LOGIN;
        } else if (path.equals("/logout")) {
            requestType = LOGOUT;
        } else if (path.equals("/update")) {
            requestType = UPDATE;
        } else if (path.equals("/resolve")) {
            requestType = RESOLVE;
        }

        if (queryMap.containsKey("session-id")) {
            sessionId = SessionId.parseSessionId(queryMap.get("session-id"));
        }

        if (!SessionId.INVALID.equals(sessionId)) {

            if (queryMap.containsKey("revision-number")) {
                revisionNumber = Integer.parseInt(queryMap.get("revision-number"));
            }

            if (queryMap.containsKey("delta")) {
                delta = Integer.parseInt(queryMap.get("delta"));
            }

            if (delta > revisionNumber) {
                throw new URISyntaxException(uri.toASCIIString(),
                        "Delta must be less or equal to revision-number: " + delta + "/" + revisionNumber);
            }

            if (queryMap.containsKey("meta")) {
                metaString = queryMap.get("meta");
            }

            isUpdateType = (delta != DaapUtil.NULL) && (delta < revisionNumber);

            // "/databases/id/items" 3 tokens
            // "/databases/id/containers" 3 tokens
            // "/databases/id/items/id.format" 4 tokens
            // "/databases/id/containers/id/items" 5 tokens
            if (path.equals("/databases")) {
                requestType = DATABASES;

            } else if (path.startsWith("/databases")) {

                StringTokenizer tok = new StringTokenizer(path, "/");
                int count = tok.countTokens();

                if (count >= 3) {
                    String token = tok.nextToken();

                    if (token.equals("databases") == false) {
                        throw new URISyntaxException(uri.toASCIIString(),
                                "Unknown token in path: " + path + " [" + token + "]@1");
                    }

                    databaseId = DaapUtil.parseUInt(tok.nextToken());
                    token = tok.nextToken();

                    if (token.equals("items")) {
                        requestType = DATABASE_SONGS;
                    } else if (token.equals("containers")) {
                        requestType = DATABASE_PLAYLISTS;
                    } else {
                        throw new URISyntaxException(uri.toASCIIString(),
                                "Unknown token in path: " + path + " [" + token + "]@2");
                    }

                    if (count == 3) {
                        // do nothing...

                    } else if (count == 4) {

                        token = tok.nextToken();

                        StringTokenizer fileTokenizer = new StringTokenizer(token, ".");

                        if (fileTokenizer.countTokens() == 2) {
                            itemId = DaapUtil.parseUInt(fileTokenizer.nextToken());
                            requestType = SONG;

                        } else {
                            throw new URISyntaxException(uri.toASCIIString(),
                                    "Unknown token in path: " + path + " [" + token + "]@3");
                        }

                    } else if (count == 5) {
                        containerId = DaapUtil.parseUInt(tok.nextToken());
                        token = tok.nextToken();

                        if (token.equals("items")) {
                            requestType = PLAYLIST_SONGS;

                        } else {
                            throw new URISyntaxException(uri.toASCIIString(),
                                    "Unknown token in path: " + path + " [" + token + "@4");
                        }

                    } else {
                        throw new URISyntaxException(uri.toASCIIString(),
                                "Unknown token in path: " + path + " [" + token + "]@5");
                    }
                } else {
                    throw new URISyntaxException(uri.toASCIIString(), "Unknown token in path: " + path);
                }
            }
        }

    } else {

        queryMap = null;
        metaString = null;
        isUpdateType = false;

        requestType = DaapUtil.NULL;
        databaseId = DaapUtil.NULL;
        containerId = DaapUtil.NULL;
        itemId = DaapUtil.NULL;

        sessionId = SessionId.INVALID;
        revisionNumber = DaapUtil.NULL;
        delta = DaapUtil.NULL;
    }
}

From source file:org.sakaiproject.lessonbuildertool.service.LessonBuilderAccessService.java

public HttpAccess getHttpAccess() {
    return new HttpAccess() {

        public void handleAccess(HttpServletRequest req, HttpServletResponse res, Reference ref,
                Collection copyrightAcceptedRefs) throws EntityPermissionException, EntityNotDefinedException,
                EntityAccessOverloadException, EntityCopyrightException {

            // preauthorized by encrypted key
            boolean isAuth = false;

            // if the id is null, the request was for just ".../content"
            String refId = ref.getId();
            if (refId == null) {
                refId = "";
            }/*w w  w .  j  ava2 s  .  c  o  m*/

            if (!refId.startsWith("/item")) {
                throw new EntityNotDefinedException(ref.getReference());
            }

            String itemString = refId.substring("/item/".length());
            // string is of form /item/NNN/url. get the number
            int i = itemString.indexOf("/");
            if (i < 0) {
                throw new EntityNotDefinedException(ref.getReference());
            }

            // get session. The problem here is that some multimedia tools don't reliably
            // pass JSESSIONID

            String sessionParam = req.getParameter("lb.session");

            if (sessionParam != null) {
                try {
                    Cipher sessionCipher = Cipher.getInstance("Blowfish");
                    sessionCipher.init(Cipher.DECRYPT_MODE, sessionKey);
                    byte[] sessionBytes = DatatypeConverter.parseHexBinary(sessionParam);
                    sessionBytes = sessionCipher.doFinal(sessionBytes);
                    String sessionString = new String(sessionBytes);
                    int j = sessionString.indexOf(":");
                    String sessionId = sessionString.substring(0, j);
                    String url = sessionString.substring(j + 1);

                    UsageSession s = UsageSessionService.getSession(sessionId);
                    if (s == null || s.isClosed() || url == null || !url.equals(refId)) {
                        throw new EntityPermissionException(sessionManager.getCurrentSessionUserId(),
                                ContentHostingService.AUTH_RESOURCE_READ, ref.getReference());
                    } else {
                        isAuth = true;
                    }

                } catch (Exception e) {
                    System.out.println("unable to decode lb.session " + e);
                }
            }

            // basically there are two checks to be done: is the item accessible in Lessons,
            // and is the underlying resource accessible in Sakai.
            // This code really does check both. Sort of. 
            // 1) it checks accessibility to the containing page by seeing if it has been visited.
            //  This is stricter than necessary, but there's no obvious reason to let people use this
            //  who aren't following an actual URL we gave them.
            // 2) it checks group access as part of the normal resource permission check. Sakai
            //  should sync the two. We actually don't check it for items in student home directories,
            //  as far as I can tell
            // 3) it checks availability (prerequisites) by calling the code from SimplePageBean
            // We could rewrite this with the new LessonsAccess methods, but since we have to do
            // resource permission checking also, and there's some duplication, it doesn't seem worth
            // rewriting this code. What I've done is review it to make sure it does the same thing.

            String id = itemString.substring(i);
            itemString = itemString.substring(0, i);

            boolean pushedAdvisor = false;

            try {
                securityService.pushAdvisor(allowReadAdvisor);

                pushedAdvisor = true;

                Long itemId = 0L;
                try {
                    itemId = (Long) Long.parseLong(itemString);
                } catch (Exception e) {
                    throw new EntityNotDefinedException(ref.getReference());
                }

                // say we've read this
                if (itemId != 0L)
                    track(itemId.longValue(), sessionManager.getCurrentSessionUserId());

                // code here is also in simplePageBean.isItemVisible. change it there
                // too if you change this logic

                SimplePageItem item = simplePageToolDao.findItem(itemId.longValue());
                SimplePage currentPage = simplePageToolDao.getPage(item.getPageId());
                String owner = currentPage.getOwner(); // if student content
                String group = currentPage.getGroup(); // if student content
                if (group != null)
                    group = "/site/" + currentPage.getSiteId() + "/group/" + group;
                String currentSiteId = currentPage.getSiteId();

                // first let's make sure the user is allowed to access
                // the containing page

                if (!isAuth && !canReadPage(currentSiteId)) {
                    throw new EntityPermissionException(sessionManager.getCurrentSessionUserId(),
                            ContentHostingService.AUTH_RESOURCE_READ, ref.getReference());
                }

                // If the resource is the actual one in the item, or
                // it is in the containing folder, then do lesson builder checking.
                // otherwise do normal resource checking

                if (!isAuth) {

                    boolean useLb = false;

                    // I've seen sakai id's with //, not sure why. they work but will mess up the comparison
                    String itemResource = item.getSakaiId().replace("//", "/");

                    // only use lb security if the user has visited the page
                    // this handles the various page release issues, although
                    // it's not quite as flexible as the real code. But I don't
                    // see any reason to help the user follow URLs that they can't
                    // legitimately have seen.

                    if (simplePageToolDao.isPageVisited(item.getPageId(),
                            sessionManager.getCurrentSessionUserId(), owner)) {
                        if (id.equals(itemResource))
                            useLb = true;
                        else {
                            // not exact, but see if it's in the containing folder
                            int endFolder = itemResource.lastIndexOf("/");
                            if (endFolder > 0) {
                                String folder = itemResource.substring(0, endFolder + 1);
                                if (id.startsWith(folder))
                                    useLb = true;
                            }
                        }
                    }

                    if (useLb) {
                        // key into access cache
                        String accessKey = itemString + ":" + sessionManager.getCurrentSessionUserId();
                        // special access if we have a student site and item is in worksite of one of the students
                        // Normally we require that the person doing the access be able to see the file, but in
                        // that specific case we allow the access. Note that in order to get a sakaiid pointing
                        // into the user's space, the person editing the page must have been able to read the file.
                        // this allows a user in your group to share any of your resources that he can see.
                        String usersite = null;
                        if (owner != null && group != null && id.startsWith("/user/")) {
                            String username = id.substring(6);
                            int slash = username.indexOf("/");
                            if (slash > 0)
                                usersite = username.substring(0, slash);
                            // normally it is /user/EID, so convert to userid
                            try {
                                usersite = UserDirectoryService.getUserId(usersite);
                            } catch (Exception e) {
                            }
                            ;
                            String itemcreator = item.getAttribute("addedby");
                            // suppose a member of the group adds a resource from another member of
                            // the group. (This will only work if they have read access to it.)
                            // We don't want to gimick access in that case. I think if you
                            // add your own item, you've given consent. But not if someone else does.
                            // itemcreator == null is for items added before this patch. I'm going to
                            // continue to allow access for them, to avoid breaking existing content.
                            if (usersite != null && itemcreator != null && !usersite.equals(itemcreator))
                                usersite = null;
                        }

                        // code here is also in simplePageBean.isItemVisible. change it there
                        // too if you change this logic

                        // for a student page, if it's in one of the groups' worksites, allow it
                        // The assumption is that only one of those people can put content in the
                        // page, and then only if the can see it.

                        if (owner != null && usersite != null
                                && authzGroupService.getUserRole(usersite, group) != null) {
                            // OK
                        } else if (owner != null && group == null && id.startsWith("/user/" + owner)) {
                            // OK
                        } else {
                            // do normal checking for other content
                            if (pushedAdvisor) {
                                securityService.popAdvisor();
                                pushedAdvisor = false;
                            }
                            // our version of allowget does not check hidden but does everything else
                            // if it's a student page, however use the normal check so students can't
                            // use this to bypass release control
                            if (owner == null && !allowGetResource(id, currentSiteId)
                                    || owner != null && !contentHostingService.allowGetResource(id)) {
                                throw new EntityPermissionException(sessionManager.getCurrentSessionUserId(),
                                        ContentHostingService.AUTH_RESOURCE_READ, ref.getReference());
                            }

                            securityService.pushAdvisor(allowReadAdvisor);
                            pushedAdvisor = true;

                        }

                        // now enforce LB access restrictions if any
                        if (item != null && item.isPrerequisite()
                                && !"true".equals((String) accessCache.get(accessKey))) {
                            // computing requirements is so messy that it's worth
                            // instantiating
                            // a SimplePageBean to do it. Otherwise we have to duplicate
                            // lots of
                            // code that changes. And we want it to be a transient bean
                            // because there are
                            // caches that we aren't trying to manage in the long term
                            // but don't do this unless the item needs checking

                            if (!canSeeAll(currentPage.getSiteId())) {
                                SimplePageBean simplePageBean = new SimplePageBean();
                                simplePageBean.setMessageLocator(messageLocator);
                                simplePageBean.setToolManager(toolManager);
                                simplePageBean.setSecurityService(securityService);
                                simplePageBean.setSessionManager(sessionManager);
                                simplePageBean.setSiteService(siteService);
                                simplePageBean.setContentHostingService(contentHostingService);
                                simplePageBean.setSimplePageToolDao(simplePageToolDao);
                                simplePageBean.setForumEntity(forumEntity);
                                simplePageBean.setQuizEntity(quizEntity);
                                simplePageBean.setAssignmentEntity(assignmentEntity);
                                simplePageBean.setBltiEntity(bltiEntity);
                                simplePageBean.setGradebookIfc(gradebookIfc);
                                simplePageBean.setMemoryService(memoryService);
                                simplePageBean.setCurrentSiteId(currentPage.getSiteId());
                                simplePageBean.setCurrentPage(currentPage);
                                simplePageBean.setCurrentPageId(currentPage.getPageId());
                                simplePageBean.init();

                                if (!simplePageBean.isItemAvailable(item, item.getPageId())) {
                                    throw new EntityPermissionException(null, null, null);
                                }
                            }
                            accessCache.put(accessKey, "true");

                        }
                    } else {

                        // normal security. no reason to use advisor
                        if (pushedAdvisor)
                            securityService.popAdvisor();
                        pushedAdvisor = false;

                        // not uselb -- their allowget, not ours. theirs checks hidden
                        if (!contentHostingService.allowGetResource(id)) {
                            throw new EntityPermissionException(sessionManager.getCurrentSessionUserId(),
                                    ContentHostingService.AUTH_RESOURCE_READ, ref.getReference());
                        }
                    }

                }

                // access checks are OK, get the thing

                // first see if it's not in resources, i.e.
                // if it doesn't start with /access/content it's something odd. redirect to it.
                // probably resources access control won't apply to it
                String url = contentHostingService.getUrl(id);
                // https://heidelberg.rutgers.edu/access/citation/content/group/24da8519-08c2-4c8c-baeb-8abdfd6c69d7/New%20Citation%20List

                int n = url.indexOf("//");
                if (n > 0) {
                    n = url.indexOf("/", n + 2);
                    if (n > 0) {
                        String path = url.substring(n);
                        if (!path.startsWith("/access/content")) {
                            res.sendRedirect(url);
                            return;
                        }
                    }
                }

                ContentResource resource = null;
                try {
                    resource = contentHostingService.getResource(id);
                } catch (IdUnusedException e) {
                    throw new EntityNotDefinedException(e.getId());
                } catch (PermissionException e) {
                    throw new EntityPermissionException(e.getUser(), e.getLock(), e.getResource());
                } catch (TypeException e) {
                    throw new EntityNotDefinedException(id);
                }

                // we only do copyright on resources. I.e. not on inline things,which are MULTIMEDIA
                if (item.getType() == SimplePageItem.RESOURCE && needsCopyright(resource)) {
                    throw new EntityCopyrightException(resource.getReference());
                }
                try {
                    // Wrap it in any filtering needed.
                    resource = contentFilterService.wrap(resource);

                    // following cast is redundant is current kernels, but is needed for Sakai 2.6.1
                    long len = (long) resource.getContentLength();
                    String contentType = resource.getContentType();

                    //    for url resource type, encode a redirect to the body URL
                    // in 2.10 have to check resourcetype, but in previous releasese
                    // it doesn't get copied in site copy, so check content type. 10 doesn't set the contenttype to url
                    // so we have to check both to work in all versions
                    if (contentType.equalsIgnoreCase(ResourceProperties.TYPE_URL)
                            || "org.sakaiproject.content.types.urlResource"
                                    .equalsIgnoreCase(resource.getResourceType())) {
                        if (len < MAX_URL_LENGTH) {
                            byte[] content = resource.getContent();
                            if ((content == null) || (content.length == 0)) {
                                throw new IdUnusedException(ref.getReference());
                            }
                            //    An invalid URI format will get caught by the
                            //    outermost catch block
                            URI uri = new URI(new String(content, "UTF-8"));
                            eventTrackingService.post(
                                    eventTrackingService.newEvent(ContentHostingService.EVENT_RESOURCE_READ,
                                            resource.getReference(null), false));
                            res.sendRedirect(uri.toASCIIString());
                        } else {
                            //    we have a text/url mime type, but the body is too
                            //    long to issue as a redirect
                            throw new EntityNotDefinedException(ref.getReference());
                        }
                    } else {

                        //    use the last part, the file name part of the id, for
                        //    the download file name
                        String fileName = Web.encodeFileName(req, Validator.getFileName(ref.getId()));

                        String disposition = null;

                        boolean inline = false;
                        if (Validator.letBrowserInline(contentType)) {
                            // type can be inline, but if HTML we have more checks to do
                            if (inlineHtml || (!"text/html".equalsIgnoreCase(contentType)
                                    && !"application/xhtml+xml".equals(contentType)))
                                // easy cases: not HTML or HTML always OK
                                inline = true;
                            else {
                                // HTML and html is not allowed globally. code copied from BaseContentServices
                                ResourceProperties rp = resource.getProperties();

                                boolean fileInline = false;
                                boolean folderInline = false;

                                try {
                                    fileInline = rp.getBooleanProperty(ResourceProperties.PROP_ALLOW_INLINE);
                                } catch (EntityPropertyNotDefinedException e) {
                                    // we expect this so nothing to do!
                                }

                                if (!fileInline)
                                    try {
                                        folderInline = resource.getContainingCollection().getProperties()
                                                .getBooleanProperty(ResourceProperties.PROP_ALLOW_INLINE);
                                    } catch (EntityPropertyNotDefinedException e) {
                                        // we expect this so nothing to do!
                                    }

                                if (fileInline || folderInline) {
                                    inline = true;
                                }
                            }
                        }

                        if (inline) {
                            disposition = "inline; filename=\"" + fileName + "\"";
                        } else {
                            disposition = "attachment; filename=\"" + fileName + "\"";
                        }

                        // NOTE: Only set the encoding on the content we have
                        // to.
                        // Files uploaded by the user may have been created with
                        // different encodings, such as ISO-8859-1;
                        // rather than (sometimes wrongly) saying its UTF-8, let
                        // the browser auto-detect the encoding.
                        // If the content was created through the WYSIWYG
                        // editor, the encoding does need to be set (UTF-8).
                        String encoding = resource.getProperties()
                                .getProperty(ResourceProperties.PROP_CONTENT_ENCODING);
                        if (encoding != null && encoding.length() > 0) {
                            contentType = contentType + "; charset=" + encoding;
                        }

                        // from contenthosting

                        res.addHeader("Cache-Control", "must-revalidate, private");
                        res.addHeader("Expires", "-1");
                        ResourceProperties rp = resource.getProperties();
                        long lastModTime = 0;

                        try {
                            Time modTime = rp.getTimeProperty(ResourceProperties.PROP_MODIFIED_DATE);
                            lastModTime = modTime.getTime();
                        } catch (Exception e1) {
                            M_log.info("Could not retrieve modified time for: " + resource.getId());
                        }

                        // KNL-1316 tell the browser when our file was last modified for caching reasons
                        if (lastModTime > 0) {
                            SimpleDateFormat rfc1123Date = new SimpleDateFormat(RFC1123_DATE, LOCALE_US);
                            rfc1123Date.setTimeZone(TimeZone.getTimeZone("GMT"));
                            res.addHeader("Last-Modified", rfc1123Date.format(lastModTime));
                        }

                        // KNL-1316 let's see if the user already has a cached copy. Code copied and modified from Tomcat DefaultServlet.java
                        long headerValue = req.getDateHeader("If-Modified-Since");
                        if (headerValue != -1 && (lastModTime < headerValue + 1000)) {
                            // The entity has not been modified since the date specified by the client. This is not an error case.
                            res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                            return;
                        }

                        ArrayList<Range> ranges = parseRange(req, res, len);

                        if (req.getHeader("Range") == null || (ranges == null) || (ranges.isEmpty())) {

                            // stream the content using a small buffer to keep memory managed
                            InputStream content = null;
                            OutputStream out = null;

                            try {
                                content = resource.streamContent();
                                if (content == null) {
                                    throw new IdUnusedException(ref.getReference());
                                }

                                res.setContentType(contentType);
                                res.addHeader("Content-Disposition", disposition);
                                res.addHeader("Accept-Ranges", "bytes");

                                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4187336
                                if (len <= Integer.MAX_VALUE) {
                                    res.setContentLength((int) len);
                                } else {
                                    res.addHeader("Content-Length", Long.toString(len));
                                }

                                // set the buffer of the response to match what we are reading from the request
                                if (len < STREAM_BUFFER_SIZE) {
                                    res.setBufferSize((int) len);
                                } else {
                                    res.setBufferSize(STREAM_BUFFER_SIZE);
                                }

                                out = res.getOutputStream();

                                copyRange(content, out, 0, len - 1);
                            } catch (ServerOverloadException e) {
                                throw e;
                            } catch (Exception ignore) {
                            } finally {
                                // be a good little program and close the stream - freeing up valuable system resources
                                if (content != null) {
                                    content.close();
                                }

                                if (out != null) {
                                    try {
                                        out.close();
                                    } catch (Exception ignore) {
                                    }
                                }
                            }

                            // Track event - only for full reads
                            eventTrackingService.post(
                                    eventTrackingService.newEvent(ContentHostingService.EVENT_RESOURCE_READ,
                                            resource.getReference(null), false));

                        } else {
                            // Output partial content. Adapted from Apache Tomcat 5.5.27 DefaultServlet.java

                            res.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);

                            if (ranges.size() == 1) {

                                // Single response

                                Range range = (Range) ranges.get(0);
                                res.addHeader("Content-Range",
                                        "bytes " + range.start + "-" + range.end + "/" + range.length);
                                long length = range.end - range.start + 1;
                                if (length < Integer.MAX_VALUE) {
                                    res.setContentLength((int) length);
                                } else {
                                    // Set the content-length as String to be able to use a long
                                    res.setHeader("content-length", "" + length);
                                }

                                res.addHeader("Content-Disposition", disposition);

                                if (contentType != null) {
                                    res.setContentType(contentType);
                                }

                                // stream the content using a small buffer to keep memory managed
                                InputStream content = null;
                                OutputStream out = null;

                                try {
                                    content = resource.streamContent();
                                    if (content == null) {
                                        throw new IdUnusedException(ref.getReference());
                                    }

                                    // set the buffer of the response to match what we are reading from the request
                                    if (len < STREAM_BUFFER_SIZE) {
                                        res.setBufferSize((int) len);
                                    } else {
                                        res.setBufferSize(STREAM_BUFFER_SIZE);
                                    }

                                    out = res.getOutputStream();

                                    copyRange(content, out, range.start, range.end);

                                } catch (ServerOverloadException e) {
                                    throw e;
                                } catch (SocketException e) {
                                    //a socket exception usualy means the client aborted the connection or similar
                                    if (M_log.isDebugEnabled()) {
                                        M_log.debug("SocketExcetion", e);
                                    }
                                } catch (Exception ignore) {
                                } finally {
                                    // be a good little program and close the stream - freeing up valuable system resources
                                    IOUtils.closeQuietly(content);
                                    IOUtils.closeQuietly(out);
                                }

                            } else {

                                // Multipart response

                                res.setContentType("multipart/byteranges; boundary=" + MIME_SEPARATOR);

                                // stream the content using a small buffer to keep memory managed
                                OutputStream out = null;

                                try {
                                    // set the buffer of the response to match what we are reading from the request
                                    if (len < STREAM_BUFFER_SIZE) {
                                        res.setBufferSize((int) len);
                                    } else {
                                        res.setBufferSize(STREAM_BUFFER_SIZE);
                                    }

                                    out = res.getOutputStream();

                                    copyRanges(resource, out, ranges.iterator(), contentType);

                                } catch (SocketException e) {
                                    //a socket exception usualy means the client aborted the connection or similar
                                    if (M_log.isDebugEnabled()) {
                                        M_log.debug("SocketExcetion", e);
                                    }
                                } catch (Exception ignore) {
                                    M_log.error("Swallowing exception", ignore);
                                } finally {
                                    // be a good little program and close the stream - freeing up valuable system resources
                                    IOUtils.closeQuietly(out);
                                }

                            } // output multiple ranges

                        } // output partial content 

                    }

                } catch (Exception t) {
                    throw new EntityNotDefinedException(ref.getReference());
                    // following won't work in 2.7.1
                    // throw new EntityNotDefinedException(ref.getReference(), t);
                }

                // not sure why we're trapping exceptions and calling them not defined, but
                // a few types are needed by the caller
            } catch (EntityCopyrightException ce) {
                // copyright exception needs to go as is, to give copyright alert
                throw ce;
            } catch (EntityPermissionException pe) {
                // also want permission exceptions; it will generate a login page
                throw pe;
            } catch (Exception ex) {
                throw new EntityNotDefinedException(ref.getReference());
            } finally {
                if (pushedAdvisor)
                    securityService.popAdvisor();
            }
        }
    };
}

From source file:org.paxle.crawler.smb.impl.SmbCrawler.java

public ICrawlerDocument request(URI requestUri) {
    if (requestUri == null)
        throw new NullPointerException("URL was null");
    this.logger.info(String.format("Crawling URL '%s' ...", requestUri));

    ICrawlerDocument crawlerDoc = null;//www  .ja  v  a  2s  .  co  m
    InputStream input = null;
    try {
        final ICrawlerContext ctx = this.contextLocal.getCurrentContext();

        // creating an empty crawler-document
        crawlerDoc = ctx.createDocument();
        crawlerDoc.setCrawlerDate(new Date());
        crawlerDoc.setLocation(requestUri);

        /* 
         * Create a temp URI to ensure that the port is set properly
         * This is required otherwise jcifs throws an exception.
         */
        URI temp = new URI(requestUri.getScheme(), requestUri.getUserInfo(), requestUri.getHost(),
                (requestUri.getPort() == -1) ? 445 : requestUri.getPort(), requestUri.getPath(),
                requestUri.getQuery(), requestUri.getFragment());

        SmbFile smbFile = new SmbFile(temp.toURL());
        if (!smbFile.exists()) {
            crawlerDoc.setStatus(Status.NOT_FOUND, "The resource does not exist");
            this.logger.info(String.format("The resource '%s' does not exit.", requestUri));
            return crawlerDoc;
        } else if (!smbFile.canRead()) {
            crawlerDoc.setStatus(Status.NOT_FOUND, "The resource can not be read.");
            this.logger.info(String.format("The resource '%s' can not be read.", requestUri));
            return crawlerDoc;
        }

        final ICrawlerTools crawlerTools = ctx.getCrawlerTools();
        if (smbFile.isDirectory()) {
            /* Append '/' if necessary. Otherwise we will get:
             * jcifs.smb.SmbException: smb://srver/dir directory must end with '/'
             */
            // XXX still needed with the SmbFile(URL)-constructor?
            String uriString = requestUri.toASCIIString();
            if (!uriString.endsWith("/")) {
                uriString += "/";
                smbFile = new SmbFile(uriString);
            }

            // set the mimetype accordingly
            crawlerDoc.setMimeType("text/html");

            // using the dir creation date as last-mod date
            long creationTimeStamp = smbFile.createTime();
            if (creationTimeStamp != 0) {
                crawlerDoc.setLastModDate(new Date(creationTimeStamp));
            }

            // getting the content of the directory
            SmbFile[] smbFiles = smbFile.listFiles();
            final Iterator<DirlistEntry> dirlistIt = new DirlistIterator(smbFiles, false);

            // generate & save dir listing
            crawlerTools.saveListing(crawlerDoc, dirlistIt, true, smbFiles.length > 50 // if more than 50 files, use compression
            );
        } else if (smbFile.isFile()) {
            // last modified timestamp
            long modTimeStamp = smbFile.getLastModified();
            if (modTimeStamp != 0) {
                crawlerDoc.setLastModDate(new Date(modTimeStamp));
            }

            // get file content
            input = smbFile.getInputStream();
        }

        if (input != null) {
            // copy data into file
            crawlerTools.saveInto(crawlerDoc, input);

            // finished
            crawlerDoc.setStatus(Status.OK);
        } else {
            crawlerDoc.setStatus(Status.UNKNOWN_FAILURE, "Unable to determine the smb-file type");
        }
    } catch (Throwable e) {
        crawlerDoc.setStatus(Status.UNKNOWN_FAILURE, "Unexpected Exception: " + e.getMessage());

        this.logger.warn(String.format("Unexpected '%s' while trying to crawl resource '%s'.",
                e.getClass().getName(), requestUri), e);
    } finally {
        if (input != null)
            try {
                input.close();
            } catch (Exception e) {
                /* ignore this */}
    }

    return crawlerDoc;
}

From source file:edu.harvard.hms.dbmi.scidb.SciDB.java

public String version() throws NotConnectedException {
    if (!this.connected) {
        throw new NotConnectedException();
    }/* ww  w. jav  a  2s  .c  o m*/

    try {
        URIBuilder uriBuilder = new URIBuilder(this.url + "/version");

        URI uri = uriBuilder.build();
        System.out.println(uri.toASCIIString());
        HttpGet runQuery = new HttpGet(uri);
        HttpResponse response = client.execute(runQuery);
        return inputStreamToString(response.getEntity().getContent());
    } catch (IOException | URISyntaxException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:edu.harvard.hms.dbmi.scidb.SciDB.java

public String newSession() throws NotConnectedException {
    if (!this.connected) {
        throw new NotConnectedException();
    }/*from   www .  j  a  va  2 s .c o  m*/

    try {
        URIBuilder uriBuilder = new URIBuilder(this.url + "/new_session");

        URI uri = uriBuilder.build();
        System.out.println(uri.toASCIIString());
        HttpGet runQuery = new HttpGet(uri);
        HttpResponse response = client.execute(runQuery);
        return inputStreamToString(response.getEntity().getContent());
    } catch (IOException | URISyntaxException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:edu.harvard.hms.dbmi.scidb.SciDB.java

public String releaseSession(String sessionId) throws NotConnectedException {
    if (!this.connected) {
        throw new NotConnectedException();
    }//from   ww  w.j av a 2 s .c om

    try {
        URIBuilder uriBuilder = new URIBuilder(this.url + "/release_session");
        uriBuilder.addParameter("id", sessionId);
        URI uri = uriBuilder.build();
        System.out.println(uri.toASCIIString());
        HttpGet runQuery = new HttpGet(uri);
        HttpResponse response = client.execute(runQuery);
        return inputStreamToString(response.getEntity().getContent());
    } catch (IOException | URISyntaxException e) {
        e.printStackTrace();
    }

    return null;
}