Some bugfixes and improvements for the PageUrlModel
parent
7e023cfaa9
commit
e228dc98ec
|
|
@ -66,6 +66,7 @@ import javax.ws.rs.Produces;
|
|||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.PathSegment;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
|
||||
|
|
@ -835,7 +836,44 @@ public class PagesController {
|
|||
}
|
||||
|
||||
private void initPageUrlModel(final UriInfo uriInfo) {
|
||||
pageUrlModel.setBasePath(uriInfo.getBaseUri().toString());
|
||||
pageUrlModel.setBaseUri(uriInfo.getBaseUri());
|
||||
pageUrlModel.setHost(uriInfo.getRequestUri().getHost());
|
||||
|
||||
final List<PathSegment> pathSegments = uriInfo.getPathSegments();
|
||||
if (pathSegments.isEmpty()) {
|
||||
throw new IllegalArgumentException("No page segements available.");
|
||||
}
|
||||
pageUrlModel.setPath(
|
||||
pathSegments
|
||||
.subList(0, pathSegments.size())
|
||||
.stream()
|
||||
.map(PathSegment::getPath)
|
||||
.collect(Collectors.joining("/"))
|
||||
);
|
||||
final String pageSegment = pathSegments
|
||||
.get(pathSegments.size() - 1)
|
||||
.getPath();
|
||||
final String[] pageTokens = pageSegment.split("\\.");
|
||||
if (pageTokens.length != 3) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format(
|
||||
"Unexpected number of tokens for page segement of path."
|
||||
+ "Expected 3 tokens, separated by '.', but got %d "
|
||||
+ "tokens.",
|
||||
pageTokens.length
|
||||
)
|
||||
);
|
||||
}
|
||||
final String pageName = pageTokens[0];
|
||||
final String pageLocale = pageTokens[1];
|
||||
final String pageFormat = pageTokens[2];
|
||||
|
||||
pageUrlModel.setPageName(pageName);
|
||||
pageUrlModel.setPageLocale(pageLocale);
|
||||
pageUrlModel.setPageFormat(pageFormat);
|
||||
|
||||
|
||||
pageUrlModel.setPath(uriInfo.getPath());
|
||||
pageUrlModel.setPort(uriInfo.getRequestUri().getPort());
|
||||
pageUrlModel.setProtocol(uriInfo.getRequestUri().getScheme());
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
*/
|
||||
package org.librecms.pages.models;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
|
@ -25,6 +26,7 @@ import java.util.stream.Collectors;
|
|||
|
||||
import javax.enterprise.context.RequestScoped;
|
||||
import javax.inject.Named;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
|
||||
/**
|
||||
* Model initialized by the Pages application containing information about the
|
||||
|
|
@ -42,10 +44,24 @@ public class PageUrlModel {
|
|||
|
||||
private int port;
|
||||
|
||||
private URI baseUri;
|
||||
|
||||
private String basePath;
|
||||
|
||||
private String path;
|
||||
|
||||
private String pageName;
|
||||
|
||||
private String pageLocale;
|
||||
|
||||
private String pageFormat;
|
||||
|
||||
private Map<String, String> queryParameters;
|
||||
|
||||
public PageUrlModel() {
|
||||
queryParameters = new HashMap<>();
|
||||
}
|
||||
|
||||
public String getProtocol() {
|
||||
return protocol;
|
||||
}
|
||||
|
|
@ -70,6 +86,22 @@ public class PageUrlModel {
|
|||
this.port = port;
|
||||
}
|
||||
|
||||
public URI getBaseUri() {
|
||||
return baseUri;
|
||||
}
|
||||
|
||||
public void setBaseUri(final URI baseUri) {
|
||||
this.baseUri = baseUri;
|
||||
}
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
}
|
||||
|
||||
public void setBasePath(final String basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
|
@ -78,8 +110,28 @@ public class PageUrlModel {
|
|||
this.path = path;
|
||||
}
|
||||
|
||||
public PageUrlModel() {
|
||||
queryParameters = new HashMap<>();
|
||||
public String getPageName() {
|
||||
return pageName;
|
||||
}
|
||||
|
||||
public void setPageName(String pageName) {
|
||||
this.pageName = pageName;
|
||||
}
|
||||
|
||||
public String getPageLocale() {
|
||||
return pageLocale;
|
||||
}
|
||||
|
||||
public void setPageLocale(String pageLocale) {
|
||||
this.pageLocale = pageLocale;
|
||||
}
|
||||
|
||||
public String getPageFormat() {
|
||||
return pageFormat;
|
||||
}
|
||||
|
||||
public void setPageFormat(String pageFormat) {
|
||||
this.pageFormat = pageFormat;
|
||||
}
|
||||
|
||||
public Map<String, String> getQueryParameters() {
|
||||
|
|
@ -94,6 +146,38 @@ public class PageUrlModel {
|
|||
this.queryParameters = new HashMap<>(queryParameters);
|
||||
}
|
||||
|
||||
public String getPageUrl() {
|
||||
final UriBuilder uriBuilder = UriBuilder
|
||||
.fromUri(baseUri)
|
||||
.path(path)
|
||||
.path(String.format("%s.%s.%s", pageName, pageLocale, pageFormat));
|
||||
|
||||
for(Map.Entry<String, String> parameter : queryParameters.entrySet()) {
|
||||
uriBuilder.queryParam(
|
||||
parameter.getKey(),
|
||||
parameter.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
return uriBuilder.toString();
|
||||
}
|
||||
|
||||
public String getPageUrl(final String locale) {
|
||||
final UriBuilder uriBuilder = UriBuilder
|
||||
.fromUri(baseUri)
|
||||
.path(path)
|
||||
.path(String.format("%s.%s.%s", pageName, locale, pageFormat));
|
||||
|
||||
for(Map.Entry<String, String> parameter : queryParameters.entrySet()) {
|
||||
uriBuilder.queryParam(
|
||||
parameter.getKey(),
|
||||
parameter.getValue()
|
||||
);
|
||||
}
|
||||
|
||||
return uriBuilder.build().toString();
|
||||
}
|
||||
|
||||
public String getQueryString() {
|
||||
if (queryParameters == null || queryParameters.isEmpty()) {
|
||||
return "";
|
||||
|
|
|
|||
Loading…
Reference in New Issue