Example usage for java.net URL getRef

List of usage examples for java.net URL getRef

Introduction

In this page you can find the example usage for java.net URL getRef.

Prototype

public String getRef() 

Source Link

Document

Gets the anchor (also known as the "reference") of this URL .

Usage

From source file:org.sakaiproject.content.tool.ResourcesAction.java

public static List<ContentResource> createUrls(SessionState state, ResourceToolActionPipe pipe) {
    logger.debug("ResourcesAction.createUrls()");
    boolean item_added = false;
    String collectionId = null;//from   ww  w.  j a v  a 2 s  .co  m
    List<ContentResource> new_resources = new ArrayList<ContentResource>();
    MultiFileUploadPipe mfp = (MultiFileUploadPipe) pipe;
    Iterator<ResourceToolActionPipe> pipeIt = mfp.getPipes().iterator();
    while (pipeIt.hasNext()) {
        ResourceToolActionPipe fp = pipeIt.next();
        collectionId = pipe.getContentEntity().getId();
        String name = fp.getFileName();
        if (name == null || name.trim().equals("")) {
            continue;
        }
        String basename = name.trim();
        String extension = ".URL";

        try {
            ContentResourceEdit resource = ContentHostingService.addResource(collectionId,
                    Validator.escapeResourceName(basename), Validator.escapeResourceName(extension),
                    MAXIMUM_ATTEMPTS_FOR_UNIQUENESS);

            extractContent(fp, resource);

            // SAK-23171 - cleanup the URL spaces
            String originalUrl = new String(resource.getContent());
            String cleanedURL = StringUtils.trim(originalUrl);
            //cleanedURL = StringUtils.replace(cleanedURL, " ", "%20");

            // SAK-23587 - properly escape the URL where required
            try {
                URL url = new URL(cleanedURL);
                URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(),
                        url.getPath(), url.getQuery(), url.getRef());
                cleanedURL = uri.toString();
            } catch (Exception e) {
                //ok to ignore, just use the original url
                logger.debug("URL can not be encoded: " + e.getClass() + ":" + e.getCause());
            }

            if (!StringUtils.equals(originalUrl, cleanedURL)) {
                // the url was cleaned up, log it and update it
                logger.info(
                        "Resources URL cleanup changed url to '" + cleanedURL + "' from '" + originalUrl + "'");
                resource.setContent(cleanedURL.getBytes());
            }

            resource.setContentType(fp.getRevisedMimeType());
            resource.setResourceType(pipe.getAction().getTypeId());
            int notification = NotificationService.NOTI_NONE;
            Object obj = fp.getRevisedListItem();
            if (obj != null && obj instanceof ListItem) {
                ((ListItem) obj).updateContentResourceEdit(resource);
                notification = ((ListItem) obj).getNotification();
            }
            ResourcePropertiesEdit resourceProperties = resource.getPropertiesEdit();
            String displayName = null;
            if (obj != null && obj instanceof ListItem) {
                displayName = ((ListItem) obj).getName();
                List<String> alerts = ((ListItem) obj).checkRequiredProperties();
                for (String alert : alerts) {
                    addAlert(state, alert);
                }
            }
            if (displayName == null || displayName.trim().equals("")) {
                displayName = name;
            }
            if (displayName != null) {
                resourceProperties.addProperty(ResourceProperties.PROP_DISPLAY_NAME, displayName);
            }
            Map values = pipe.getRevisedResourceProperties();
            for (Iterator<Entry<String, String>> mapIter = values.entrySet().iterator(); mapIter.hasNext();) {
                Entry<String, String> entry = mapIter.next();
                resourceProperties.addProperty(entry.getKey(), entry.getValue());
            }
            try {
                ContentHostingService.commitResource(resource, notification);
                conditionsHelper.notifyCondition(resource);
                item_added = true;
                new_resources.add(resource);
            } catch (OverQuotaException e) {
                addAlert(state, trb.getFormattedMessage("alert.overquota", new String[] { name }));
                logger.debug("OverQuotaException " + e);
                try {
                    ContentHostingService.removeResource(resource.getId());
                } catch (Exception e1) {
                    logger.debug(
                            "Unable to remove partially completed resource: " + resource.getId() + "\n" + e);
                }
            } catch (ServerOverloadException e) {
                addAlert(state, trb.getFormattedMessage("alert.unable1", new String[] { name }));
                logger.debug("ServerOverloadException " + e);
                try {
                    ContentHostingService.removeResource(resource.getId());
                } catch (Exception e1) {
                    logger.debug(
                            "Unable to remove partially completed resource: " + resource.getId() + "\n" + e);
                }
            }
        } catch (PermissionException e) {
            addAlert(state, trb.getString("alert.perm"));
            logger.warn("PermissionException ", e);
        } catch (IdUnusedException e) {
            logger.warn("IdUsedException ", e);
        } catch (IdInvalidException e) {
            logger.warn("IdInvalidException ", e);
        } catch (IdUniquenessException e) {
            logger.warn("IdUniquenessException ", e);
        } catch (IdLengthException e) {
            addAlert(state, trb.getFormattedMessage("alert.toolong", new String[] { e.getMessage() }));
            // TODO Auto-generated catch block
            logger.warn("IdLengthException ", e);
        } catch (OverQuotaException e) {
            addAlert(state, trb.getFormattedMessage("alert.overquota", new String[] { name }));
            logger.warn("OverQuotaException ", e);
        } catch (ServerOverloadException e) {
            addAlert(state, trb.getFormattedMessage("alert.unable1", new String[] { name }));
            logger.warn("ServerOverloadException ", e);
        }
    }

    return (item_added ? new_resources : null);
}