private JerseyUriBuilder resolveTemplate(final String name, final Object value, final boolean encode, final boolean encodeSlashInPath) { if (name == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("name")); } if (value == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("value")); } final Map<String, Object> templateValues = new HashMap<>(); templateValues.put(name, value); resolveTemplates(templateValues, encode, encodeSlashInPath); return this; }
private void appendPath(final Path path) { if (path == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("path")); } appendPath(path.value()); }
private JerseyUriBuilder resolveTemplate(final String name, final Object value, final boolean encode, final boolean encodeSlashInPath) { if (name == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("name")); } if (value == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("value")); } final Map<String, Object> templateValues = new HashMap<>(); templateValues.put(name, value); resolveTemplates(templateValues, encode, encodeSlashInPath); return this; }
private void appendPath(final Path path) { if (path == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("path")); } appendPath(path.value()); }
@Override public JerseyUriBuilder segment(final String... segments) throws IllegalArgumentException { checkSsp(); if (segments == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("segments")); } for (final String segment : segments) { appendPath(segment, true); } return this; }
@Override public JerseyUriBuilder segment(final String... segments) throws IllegalArgumentException { checkSsp(); if (segments == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("segments")); } for (final String segment : segments) { appendPath(segment, true); } return this; }
@SuppressWarnings("unchecked") @Override public UriBuilder path(final Class resource) throws IllegalArgumentException { checkSsp(); if (resource == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("resource")); } final Path p = Path.class.cast(resource.getAnnotation(Path.class)); if (p == null) { throw new IllegalArgumentException(LocalizationMessages.URI_BUILDER_CLASS_PATH_ANNOTATION_MISSING(resource)); } appendPath(p); return this; }
@SuppressWarnings("unchecked") @Override public UriBuilder path(final Class resource) throws IllegalArgumentException { checkSsp(); if (resource == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("resource")); } final Path p = Path.class.cast(resource.getAnnotation(Path.class)); if (p == null) { throw new IllegalArgumentException(LocalizationMessages.URI_BUILDER_CLASS_PATH_ANNOTATION_MISSING(resource)); } appendPath(p); return this; }
private void appendPath(String segments, final boolean isSegment) { if (segments == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("segments")); } if (segments.isEmpty()) { return; } // Encode matrix parameters on current path segment encodeMatrix(); segments = encode(segments, (isSegment) ? UriComponent.Type.PATH_SEGMENT : UriComponent.Type.PATH); final boolean pathEndsInSlash = path.length() > 0 && path.charAt(path.length() - 1) == '/'; final boolean segmentStartsWithSlash = segments.charAt(0) == '/'; if (path.length() > 0 && !pathEndsInSlash && !segmentStartsWithSlash) { path.append('/'); } else if (pathEndsInSlash && segmentStartsWithSlash) { segments = segments.substring(1); if (segments.isEmpty()) { return; } } path.append(segments); }
private void appendPath(String segments, final boolean isSegment) { if (segments == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("segments")); } if (segments.isEmpty()) { return; } // Encode matrix parameters on current path segment encodeMatrix(); segments = encode(segments, (isSegment) ? UriComponent.Type.PATH_SEGMENT : UriComponent.Type.PATH); final boolean pathEndsInSlash = path.length() > 0 && path.charAt(path.length() - 1) == '/'; final boolean segmentStartsWithSlash = segments.charAt(0) == '/'; if (path.length() > 0 && !pathEndsInSlash && !segmentStartsWithSlash) { path.append('/'); } else if (pathEndsInSlash && segmentStartsWithSlash) { segments = segments.substring(1); if (segments.isEmpty()) { return; } } path.append(segments); }
@Override public JerseyUriBuilder path(final Method method) { checkSsp(); if (method == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("method")); } appendPath(getPath(method)); return this; }
@Override public JerseyUriBuilder path(final Method method) { checkSsp(); if (method == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("method")); } appendPath(getPath(method)); return this; }
@Override public JerseyUriBuilder path(final Class resource, final String methodName) { checkSsp(); if (resource == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("resource")); } if (methodName == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("methodName")); } final Method[] methods = AccessController.doPrivileged(ReflectionHelper.getMethodsPA(resource)); Method found = null; for (final Method m : methods) { if (methodName.equals(m.getName())) { if (found == null || found.isSynthetic()) { found = m; } else if (!m.isSynthetic()) { throw new IllegalArgumentException(); } } } if (found == null) { throw new IllegalArgumentException(LocalizationMessages.URI_BUILDER_METHODNAME_NOT_SPECIFIED(methodName, resource)); } appendPath(getPath(found)); return this; }
@Override public JerseyUriBuilder path(final Class resource, final String methodName) { checkSsp(); if (resource == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("resource")); } if (methodName == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("methodName")); } final Method[] methods = AccessController.doPrivileged(ReflectionHelper.getMethodsPA(resource)); Method found = null; for (final Method m : methods) { if (methodName.equals(m.getName())) { if (found == null || found.isSynthetic()) { found = m; } else if (!m.isSynthetic()) { throw new IllegalArgumentException(); } } } if (found == null) { throw new IllegalArgumentException(LocalizationMessages.URI_BUILDER_METHODNAME_NOT_SPECIFIED(methodName, resource)); } appendPath(getPath(found)); return this; }
@Override public JerseyUriBuilder replaceMatrixParam(String name, final Object... values) { checkSsp(); if (name == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("name")); } if (matrixParams == null) { int i = path.lastIndexOf("/"); if (i == -1) { i = 0; } matrixParams = UriComponent.decodeMatrix(path.substring(i), false); i = path.indexOf(";", i); if (i != -1) { path.setLength(i); } } name = encode(name, UriComponent.Type.MATRIX_PARAM); matrixParams.remove(name); if (values != null) { for (final Object value : values) { if (value == null) { throw new IllegalArgumentException(LocalizationMessages.MATRIX_PARAM_NULL()); } matrixParams.add(name, encode(value.toString(), UriComponent.Type.MATRIX_PARAM)); } } return this; }
@Override public JerseyUriBuilder replaceMatrixParam(String name, final Object... values) { checkSsp(); if (name == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("name")); } if (matrixParams == null) { int i = path.lastIndexOf("/"); if (i == -1) { i = 0; } matrixParams = UriComponent.decodeMatrix(path.substring(i), false); i = path.indexOf(";", i); if (i != -1) { path.setLength(i); } } name = encode(name, UriComponent.Type.MATRIX_PARAM); matrixParams.remove(name); if (values != null) { for (final Object value : values) { if (value == null) { throw new IllegalArgumentException(LocalizationMessages.MATRIX_PARAM_NULL()); } matrixParams.add(name, encode(value.toString(), UriComponent.Type.MATRIX_PARAM)); } } return this; }
private JerseyUriBuilder resolveTemplates(final Map<String, Object> templateValues, final boolean encode, final boolean encodeSlashInPath) { if (templateValues == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("templateValues")); } else { for (final Map.Entry entry : templateValues.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) { throw new IllegalArgumentException(LocalizationMessages.TEMPLATE_PARAM_NULL()); } } } scheme = UriTemplate.resolveTemplateValues(UriComponent.Type.SCHEME, scheme, false, templateValues); userInfo = UriTemplate.resolveTemplateValues(UriComponent.Type.USER_INFO, userInfo, encode, templateValues); host = UriTemplate.resolveTemplateValues(UriComponent.Type.HOST, host, encode, templateValues); port = UriTemplate.resolveTemplateValues(UriComponent.Type.PORT, port, false, templateValues); authority = UriTemplate.resolveTemplateValues(UriComponent.Type.AUTHORITY, authority, encode, templateValues); // path template values are treated as path segments unless encodeSlashInPath is false. final UriComponent.Type pathComponent = (encodeSlashInPath) ? UriComponent.Type.PATH_SEGMENT : UriComponent.Type.PATH; final String newPath = UriTemplate.resolveTemplateValues(pathComponent, path.toString(), encode, templateValues); path.setLength(0); path.append(newPath); final String newQuery = UriTemplate.resolveTemplateValues(UriComponent.Type.QUERY_PARAM, query.toString(), encode, templateValues); query.setLength(0); query.append(newQuery); fragment = UriTemplate.resolveTemplateValues(UriComponent.Type.FRAGMENT, fragment, encode, templateValues); return this; }
private JerseyUriBuilder resolveTemplates(final Map<String, Object> templateValues, final boolean encode, final boolean encodeSlashInPath) { if (templateValues == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("templateValues")); } else { for (final Map.Entry entry : templateValues.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) { throw new IllegalArgumentException(LocalizationMessages.TEMPLATE_PARAM_NULL()); } } } scheme = UriTemplate.resolveTemplateValues(UriComponent.Type.SCHEME, scheme, false, templateValues); userInfo = UriTemplate.resolveTemplateValues(UriComponent.Type.USER_INFO, userInfo, encode, templateValues); host = UriTemplate.resolveTemplateValues(UriComponent.Type.HOST, host, encode, templateValues); port = UriTemplate.resolveTemplateValues(UriComponent.Type.PORT, port, false, templateValues); authority = UriTemplate.resolveTemplateValues(UriComponent.Type.AUTHORITY, authority, encode, templateValues); // path template values are treated as path segments unless encodeSlashInPath is false. final UriComponent.Type pathComponent = (encodeSlashInPath) ? UriComponent.Type.PATH_SEGMENT : UriComponent.Type.PATH; final String newPath = UriTemplate.resolveTemplateValues(pathComponent, path.toString(), encode, templateValues); path.setLength(0); path.append(newPath); final String newQuery = UriTemplate.resolveTemplateValues(UriComponent.Type.QUERY_PARAM, query.toString(), encode, templateValues); query.setLength(0); query.append(newQuery); fragment = UriTemplate.resolveTemplateValues(UriComponent.Type.FRAGMENT, fragment, encode, templateValues); return this; }
@Override public JerseyUriBuilder uri(final String uriTemplate) { if (uriTemplate == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("uriTemplate")); } UriParser parser = new UriParser(uriTemplate); parser.parse(); final String parsedScheme = parser.getScheme(); if (parsedScheme != null) { scheme(parsedScheme); } else if (ssp != null) { // The previously set scheme was opaque and uriTemplate does not contain a scheme part. // However, the scheme might have already changed, as demonstrated in // JerseyUriBuilderTest.testChangeUriStringAfterChangingOpaqueSchemeToHttp(). // So to be safe, we need to erase the existing internal SSP value and // re-parse the new uriTemplate using the current scheme and try to set the SSP // again using the re-parsed data. // See also JERSEY-1457 and related test. ssp = null; parser = new UriParser(scheme + ":" + uriTemplate); parser.parse(); } schemeSpecificPart(parser); final String parserFragment = parser.getFragment(); if (parserFragment != null) { fragment(parserFragment); } return this; }
@Override public JerseyUriBuilder uri(final String uriTemplate) { if (uriTemplate == null) { throw new IllegalArgumentException(LocalizationMessages.PARAM_NULL("uriTemplate")); } UriParser parser = new UriParser(uriTemplate); parser.parse(); final String parsedScheme = parser.getScheme(); if (parsedScheme != null) { scheme(parsedScheme); } else if (ssp != null) { // The previously set scheme was opaque and uriTemplate does not contain a scheme part. // However, the scheme might have already changed, as demonstrated in // JerseyUriBuilderTest.testChangeUriStringAfterChangingOpaqueSchemeToHttp(). // So to be safe, we need to erase the existing internal SSP value and // re-parse the new uriTemplate using the current scheme and try to set the SSP // again using the re-parsed data. // See also JERSEY-1457 and related test. ssp = null; parser = new UriParser(scheme + ":" + uriTemplate); parser.parse(); } schemeSpecificPart(parser); final String parserFragment = parser.getFragment(); if (parserFragment != null) { fragment(parserFragment); } return this; }