Resolve Confluence Page urls like Space:Page Title
Asked by developerrespig:

Hey guys,

sorry in advance for the fact that I don't know if this is a duplicate question, if so, then please point me to the original one.

So my problem is, that I try to get every outgoing link for a page. This isn't a problem for me so far since there is a method

```
page.getOutgoingLinks()
```

which does exactly that. But the problem I'm currently running into is that I receive urls like Space:Page Title and that I can't find a way to convert them to a real url. Can anyone help me and point me to a class that converts this format into an url?

Thank you in advance.


Answers:
author: developerrespig
body: Just copied my answer from a discussion to be able to mark it as a solution. Thanks for the help that I received :)

Thank you for the hint. I digged through the code and had a talk with another colleague and I think I found a solution.

So option one in this case would be using the

```
wikiStyleRenderer.convertWikiToXHtml(renderContext, "[" + entity.getOutgoingLinks().get(0).getUrlLink() + "]");
```

which should be pretty much self explaining. I guess this method uses the same mechanic as you suggested with the usage of the storage format. The problem here is, that I get something like

```
<p><a href="link_to_page">title</a></p>
```

The thing that bugs me is the \<p\> around the link since this destroys other styling that I'm wrapping around the link (like an icon in front of it). But this could be a way for other users so that's why I'm mentioning it here.

The way I'm going for now is the option with the page manager. As mentioned before I digged through the code and found the LinkRenderer, which needs a Link object and a context. Further digging lead me to a PageLink object, which needs a GenericLinkParser, a context and the page manager. After checking the implementation I saw that the PageLink object uses the page manager to get the page via the given space key and its title.

So I will use the page manager directly.

Another hint: If you want to parse a wiki markup url like spacekey:page title, you can use the GenericLinkParser since it has a method called "parseAsContent". This method parses the given String and then you can request the spacekey and page title out of it without having to write your own code :)


author: Christo Mastoroudes _Adaptavist_
body: Hi,

I had a quick look and it seems that

```
page.getOutgoingLinks()
```

returns objects of type OutgoingLink. I think you could try to call

```
getUrlPath()
```

on one of the objects you received from executing the first line. Let me know the result.

If this answers your question please mark it as answered.

comments: author: developerrespig
body: Hey,

thank you for your answer. Sadly I can't find a method on the OutgoingLink object which is called getUrlPath(). There is a method called getUrlLink(), but this one only returns the previously mentioned link format of spacekey:page title or am I doing anything wrong?


author: Christo Mastoroudes _Adaptavist_
body: Aah yes, upon closer investigation you are right it is called getUrlLink() and it does not return a qualified link but instead spacekey:page as you mentioned.

I think you can build your own link in this case.

Inject the SettingsManager and try something like this:

```
String baseUrl = settingsManager.getGlobalSettings().getBaseUrl()

String pageLink = baseUrl + "/display/" + outgoingLink.getDestinationSpaceKey() + "/" + outgoingLink.getDestinationPageTitle()
```


author: developerrespig
body: This is an option, but this leaves me with problems when using special characters like "?" in the page title. This solution would then fail. I mean I could also try to get the page via the PageManager und then build a url with the pages id, but this kinda feels wrong since there must be a frontend function that handles such Confluence wiki markup links.


author: Christo Mastoroudes _Adaptavist_
body: Correct, a robust solution would require the use of a page id. Unfortunately I can't see any solution provided by the OutgoingLink class.

Have you tried looking at LinkManager.extractLinksFromContent() ?


author: developerrespig
body: Not yet but I just checked it. The problem I see there is that the extractLinksFromContent method is deprecated and replaced by the OutgoingLinksExtractor. But this class only extracts OutgoingLinks from a content entity object which leaves me with the same problem :(

Thank you for your help and suggestions so far. It is really appreciated :)


author: Christo Mastoroudes _Adaptavist_
body: You are welcome, sorry I don't have better answers :)

I am not sure about your use case but would it not simply be possible for you to use the OutgoingLink returned instead of trying to resolve it to a url? Let Confluence do the work for you.

There must be a mechanism for resolving the OutgoingLink in the Confluence code but I am not sure it would be any better than manually building the url, and scratching through the source code to find it could be long.

I say this because you can put a link on a page with storage format:

```
<ac:link><ri:page ri:content-title="p2" /></ac:link>
```

and Confluence will resolve this to a url most likely using a simple url builder.


author: developerrespig
body: Thank you for the hint. I digged through the code and had a talk with another colleague and I think I found a solution.

So option one in this case would be using the

```
wikiStyleRenderer.convertWikiToXHtml(renderContext, "[" + entity.getOutgoingLinks().get(0).getUrlLink() + "]");
```

which should be pretty much self explaining. I guess this method uses the same mechanic as you suggested with the usage of the storage format. The problem here is, that I get something like

```
<p><a href="link_to_page">title</a></p>
```

The thing that bugs me is the \<p\> around the link since this destroys other styling that I'm wrapping around the link (like an icon in front of it). But this could be a way for other users so that's why I'm mentioning it here.

The way I'm going for now is the option with the page manager. As mentioned before I digged through the code and found the LinkRenderer, which needs a Link object and a context. Further digging lead me to a PageLink object, which needs a GenericLinkParser, a context and the page manager. After checking the implementation I saw that the PageLink object uses the page manager to get the page via the given space key and its title.

So I will use the page manager directly.

Another hint: If you want to parse a wiki markup url like spacekey:page title, you can use the GenericLinkParser since it has a method called "parseAsContent". This method parses the given String and then you can request the spacekey and page title out of it without having to write your own code :)

Thank you for your help so far! The problem is now solved for me :)


author: developerrespig
body: please delete


Tags: developer