Example usage for com.google.gwt.safehtml.shared UriUtils sanitizeUri

List of usage examples for com.google.gwt.safehtml.shared UriUtils sanitizeUri

Introduction

In this page you can find the example usage for com.google.gwt.safehtml.shared UriUtils sanitizeUri.

Prototype

public static String sanitizeUri(String uri) 

Source Link

Document

Sanitizes a URI.

Usage

From source file:cc.kune.common.shared.utils.TextUtils.java

License:GNU Affero Public License

/**
 * Generates a href link.//from  ww  w.jav  a 2s . c o m
 *
 * @param href
 *          the href
 * @param text
 *          the text
 * @param targetBlank
 *          the target blank
 * @return the string
 */
public static String generateHtmlLink(final String href, final String text, final boolean targetBlank) {
    if (!UriUtils.isSafeUri(href)) {
        throw new UIException("Unsafe href");
    }
    return "<a href=\"" + UriUtils.sanitizeUri(href) + "\"" + (targetBlank ? "target=\"_blank\"" : "") + ">"
            + text + "</a>";
}

From source file:mx.org.pescadormvp.core.client.internallinks.InternalLinkBase.java

License:Open Source License

@Override
public void setPlace(PescadorMVPPlace place) {
    this.place = place;
    setHref(UriUtils.sanitizeUri((place.getURL())));

    if (alternatePresentationText == null) {
        // there may be cases, perhaps due to malicious scripts, or
        // otherwise, in which a place's presentationText is null.
        // escaping presentation text is subclass's responsibility
        String presentationText = place.getPresentationText();
        if (presentationText != null)
            setPresentationText(presentationText);
    }/*from  w w  w . j a  v a  2  s. co m*/
}

From source file:mx.org.pescadormvp.core.client.internallinks.InternalLinkHTMLRenderer.java

License:Open Source License

/**
 * Set the URL for the link. 
 */
public void setHref(String href) {
    this.href = UriUtils.sanitizeUri(href);
}

From source file:mx.org.pescadormvp.examples.jsonp.client.query.GetLatLonActionHelperImpl.java

License:Open Source License

@Override
public String getRequestURL(GetLatLonAction action) {
    return UriUtils.sanitizeUri(BASE_URL + action.getLocation());
}

From source file:org.eclipse.che.plugin.tour.client.action.impl.OpenURLExternalAction.java

License:Open Source License

/**
 * Open URL link (after performing a check).
 * @param link the URL link to open/*from   w  ww. ja  v a  2 s . c o  m*/
 */
@Override
public void execute(String link) {
    String uri = UriUtils.sanitizeUri(link);
    log.debug("Opening URI {0}", uri);
    Window.open(uri, "_blank", "");
}

From source file:org.eclipse.che.plugin.tour.client.html.CustomImage.java

License:Open Source License

/**
 * Add the markdown images from the given text
 * @param text the text to parse/*  w  w  w.  j av  a2  s .c o m*/
 * @return the string with images added inside
 */
public String addImages(String text) {

    RegExp REGEXP_IMAGES = RegExp.compile("!\\[(.*?)\\]\\((.+?)\\)", "g");
    RegExp REGEXP_IMAGE_DETAIL = RegExp.compile("(.*?)\\s*=\\s*(\\d+)x(\\d*)", "g");

    //Image format : ![](./pic/pic1_50.png =100x20)
    MatchResult matchResult = REGEXP_IMAGES.exec(text);

    if (matchResult != null && matchResult.getGroupCount() == 3) {
        String alt = matchResult.getGroup(1);
        String uri = matchResult.getGroup(2);
        int width = -1;
        int height = -1;

        MatchResult imgDetails = REGEXP_IMAGE_DETAIL.exec(uri);
        if (imgDetails != null && imgDetails.getGroupCount() == 4) {
            uri = imgDetails.getGroup(1);
            width = Integer.parseInt(imgDetails.getGroup(2));
            String valHeight = imgDetails.getGroup(3);
            if (!valHeight.isEmpty()) {
                height = Integer.parseInt(imgDetails.getGroup(3));
            }
        }

        StringBuilder imageData = new StringBuilder();
        imageData.append("<img src='");
        imageData.append(UriUtils.sanitizeUri(uri));
        imageData.append("' alt='");
        imageData.append(SafeHtmlUtils.fromString(alt).asString());
        imageData.append("'");
        if (width != -1) {
            imageData.append(" width='");
            imageData.append(width);
            imageData.append("'");
        }
        if (height != -1) {
            imageData.append(" height='");
            imageData.append(height);
            imageData.append("'");
        }

        imageData.append(" />");

        return text.replace(matchResult.getGroup(0), imageData.toString());
    }

    return text;
}

From source file:org.eclipse.che.plugin.tour.client.html.WikiHyperLinks.java

License:Open Source License

/**
 * Add the wiki hyperlink result from the given text
 * @param text the text to parse//from w  w w  .j av  a  2 s . c  om
 * @return the string with hyperlinks added inside
 */
public String addLinks(String text) {

    RegExp REGEXP_LINKS = RegExp.compile("\\[.*?\\s*(.*?)\\]", "g");

    // wiki link : [http://www.codenvy.com] or [http://www.codenvy.com web site]
    MatchResult matchResult = REGEXP_LINKS.exec(text);

    if (matchResult != null && matchResult.getGroupCount() == 2) {
        String content = matchResult.getGroup(1);

        StringBuilder sb = new StringBuilder("<a href=\"");
        int space = content.indexOf(' ');
        if (space == -1) {
            sb.append(UriUtils.sanitizeUri(content));
            sb.append("\">");
            sb.append(UriUtils.sanitizeUri(content));
        } else {
            sb.append(UriUtils.sanitizeUri(content.substring(0, space)));
            sb.append("\">");
            sb.append(content.substring(space + 1, content.length()));
        }
        sb.append("</a>");

        String hyperLinksValue = text.replace(matchResult.getGroup(0), sb.toString());
        // Check also if there are other links
        return addLinks(hyperLinksValue);
    }

    return text;
}

From source file:org.eclipse.che.plugin.tour.client.tour.GuidedTour.java

License:Open Source License

/**
 * Adds the given overlay.//from   ww  w .  j a  v  a2  s .  c  o m
 * @param overlay the details of the overlay
 */
protected void addOverlay(GuidedTourImageOverlay overlay) {

    int top = 0;
    int left = 0;

    // First, check if element is there
    String elementToCheckName = overlay.getElement();
    if (elementToCheckName != null && !elementToCheckName.isEmpty()) {
        Element elementToCheck = Document.get().getElementById(elementToCheckName);
        if (elementToCheck == null) {
            log.error("The element " + elementToCheckName + " does not exist, skipping overlay");
            return;
        }

        if (overlay.getUrl() == null) {
            log.error("No URL given for image URL, skipping overlay");
            return;
        }

        // add element position
        top = elementToCheck.getAbsoluteTop();
        left = elementToCheck.getAbsoluteLeft();
    }

    // do we have shift offset ?
    String xOffsetStr = overlay.getXOffset();
    if (xOffsetStr != null && !xOffsetStr.isEmpty()) {
        try {
            left = left + Integer.parseInt(xOffsetStr);
        } catch (NumberFormatException e) {
            log.error("Invalid xOffset" + xOffsetStr);
        }
    }

    String yOffsetStr = overlay.getYOffset();
    if (yOffsetStr != null && !yOffsetStr.isEmpty()) {
        try {
            top = top + Integer.parseInt(yOffsetStr);
        } catch (NumberFormatException e) {
            log.error("Invalid yOffset" + yOffsetStr);
        }
    }

    Widget widget;

    // Create a Image widget if there is an URL
    if (overlay.getUrl() != null) {
        Image image = new Image();

        // Set image URL
        image.setUrl(UriUtils.sanitizeUri(overlay.getUrl()));
        widget = image;

    } else {
        widget = new HTML();
    }

    Element widgetElement = widget.getElement();

    // absolute position
    widgetElement.getStyle().setTop(top, Style.Unit.PX);
    widgetElement.getStyle().setLeft(left, Style.Unit.PX);
    widgetElement.getStyle().setPosition(Style.Position.ABSOLUTE);

    // default index
    int zIndex = 5;
    String zIndexStr = overlay.getZIndex();
    if (zIndexStr != null && !zIndexStr.isEmpty()) {
        try {
            zIndex = Integer.parseInt(zIndexStr);
        } catch (NumberFormatException e) {
            log.error("Invalid zIndex" + zIndexStr);
        }
    }
    widgetElement.getStyle().setZIndex(zIndex);

    // custom width/height ?

    SizeAttribute width = overlay.getWidth();
    SizeAttribute height = overlay.getHeight();
    if (width != null) {
        int value = width.getValue();
        String unitString = width.getUnit();
        if ("%".equals(unitString)) {
            unitString = "PCT";
        }
        Style.Unit unit = Style.Unit.valueOf(unitString);
        widgetElement.getStyle().setWidth(value, unit);
    }
    if (height != null) {
        int value = height.getValue();
        String unitString = height.getUnit();
        if ("%".equals(unitString)) {
            unitString = "PCT";
        }
        Style.Unit unit = Style.Unit.valueOf(unitString);
        widgetElement.getStyle().setHeight(value, unit);
    }

    // background color
    String bgColor = overlay.getBackgroundColor();
    if (bgColor != null) {
        widgetElement.getStyle().setBackgroundColor(bgColor);
    }

    // add image
    RootPanel.get().add(widget);
    currentOverlays.add(widget);

    // image should disappear on click
    if (widget instanceof HasClickHandlers) {
        HasClickHandlers hasClickHandlers = (HasClickHandlers) widget;
        hasClickHandlers.addClickHandler(new RemoveWidgetClickHandler(widget));
    }

}