Patrick Kwinten
Patrick Kwinten

Reputation: 2048

build url's in java in xpages

in xpages I can use:

var protocol = context.getUrl().getScheme() + "://";
var url:XSPUrl = new XSPUrl(database.getHttpURL());
var host = url.getHost();
...

to build urls to documents/files in documents

How should I build the URL's equivalent in Java?

Upvotes: 1

Views: 264

Answers (2)

David Leedy
David Leedy

Reputation: 3593

I build URL's in Java all the time.

Here are some RANDOM code snippets that I use. This is not a single class, just random snippets that should help you get started at least.

FacesContext facesContext = FacesContext.getCurrentInstance();
XSPContext context = XSPContext.getXSPContext(facesContext);

String entryPage = context.getUrl().getPath() + context.getUrl().getQueryString();

if (entryPage.contains("/home.xsp")) {
    this.console("Entry Page contains /home.xsp");
    if (this.isBasicMode()) {
            entryPage.replace("home.xsp", "basic_Menu.xsp");
        }
    } else {
        this.console("entry page does NOT contain /home.xsp");
    }

context.redirectToPage(“/myPage.xsp”);


public void redirectExternal(String url) throws IOException {

    FacesContext fc = FacesContext.getCurrentInstance();
        ExtenalContext externalContext = fc.getExternalContext();
        externalContext.redirect(url);

    }

public void redirectToPage(final String pageName) {
        // pageName = "/myPage.xsp"

        try {
            // You'd think this would end all Java processing but that's NOT
            // what happens
            // It looks like the Java code will finish and only then will the
            // redirection happen.

            final String entryPage = JSFUtil.getXSPContext().getUrl().getPath() + JSFUtil.getXSPContext().getUrl().getQueryString();
            FrameworkUtils.getSessionScope().put("entryPage", entryPage);
            this.setEntryPage(entryPage);

            JSFUtil.getXSPContext().redirectToPage(pageName);

        } catch (final RedirectSignal rs) {
            // Ignoring this error. Useless!
        }
        // Returning false so we can stop the calling code from continuing
        // return false;

    }

    public String getParam(final String key) {
        if (!this.getQueryString().containsKey(key)) {
            return null;
        } else {
            return this.getQueryString().get(key);
        }

    }


    @SuppressWarnings("unchecked")
    public Map<String, String> getQueryString() {
        final Map<String, String> qs = (Map<String, String>) FrameworkUtils.resolveVariable("param");
        return qs;
    }

Upvotes: 3

Frank van der Linden
Frank van der Linden

Reputation: 1271

depends where your documents files are. It think the best way is create a servlet in the nsf, call that by passing an docUNID and attachmentName. Then you can get the document and write the attachment as stream to the response. If you set the header to attachment, like

response.setHeader("Content-disposition", "attachment; filename=\""+attachment.getName()+"\"");

you will get the attachment as download

Upvotes: 1

Related Questions