java getpathinfo_Java URIBuilder.getPath方法代码示例

这段代码展示了如何使用Java的URIBuilder类来构建URI,特别是处理getPathInfo和是否移除最后一个路径段的情况。首先,它创建了一个URIBuilder实例,然后获取请求的路径信息。如果路径不以指定前缀开始,它会添加前缀。根据`remove_last_segment`的值,决定是否删除路径的最后一个段或者`$links`段。最后,设置URIBuilder的路径并返回构建的URI。
摘要由CSDN通过智能技术生成

import org.apache.http.client.utils.URIBuilder; //导入方法依赖的package包/类

private URI makeLink(boolean remove_last_segment) throws ODataException

{

try

{

URIBuilder ub = new URIBuilder(ServiceFactory.EXTERNAL_URL);

StringBuilder sb = new StringBuilder();

String prefix = ub.getPath();

String path = getContext().getPathInfo().getRequestUri().getPath();

if (path == null || path.isEmpty() ||

prefix != null && !prefix.isEmpty() && !path.startsWith(ub.getPath()))

{

sb.append(prefix);

if (path != null)

{

if (prefix.endsWith("/") && path.startsWith("/"))

{

sb.deleteCharAt(sb.length() - 1);

}

if (!prefix.endsWith("/") && !path.startsWith("/"))

{

sb.append('/');

}

}

}

sb.append(path);

if (remove_last_segment)

{

// Removes the last segment.

int lio = sb.lastIndexOf("/");

while (lio != -1 && lio == sb.length() - 1)

{

sb.deleteCharAt(lio);

lio = sb.lastIndexOf("/");

}

if (lio != -1)

{

sb.delete(lio + 1, sb.length());

}

// Removes the `$links` segment.

lio = sb.lastIndexOf("$links/");

if (lio != -1)

{

sb.delete(lio, lio + 7);

}

}

else if (!sb.toString().endsWith("/") && !sb.toString().endsWith("\\"))

{

sb.append("/");

}

ub.setPath(sb.toString());

return ub.build();

}

catch (NullPointerException | URISyntaxException e)

{

throw new ODataException(e);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值