/** * An alternative to {@link #fromMethodName(Class, String, Object...)}} for * use with an instance of this class created via {@link #relativeTo}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @since 4.2 */ public UriComponentsBuilder withMethodName(Class<?> controllerType, String methodName, Object... args) { return fromMethodName(this.baseUrl, controllerType, methodName, args); }
/** * An alternative to {@link #fromMethodName(Class, String, Object...)}} for * use with an instance of this class created via {@link #relativeTo}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @since 4.2 */ public UriComponentsBuilder withMethodName(Class<?> controllerType, String methodName, Object... args) { return fromMethodName(this.baseUrl, controllerType, methodName, args); }
@GetMapping("/") public String listUploadedFiles(Model model) throws IOException { model.addAttribute("files", storageService .loadAll() .map(path -> MvcUriComponentsBuilder .fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString()) .build().toString()) .collect(Collectors.toList())); return "uploadForm"; }
@Test public void fromMethodNameNotMapped() { UriComponents uriComponents = fromMethodName(UnmappedController.class, "unmappedMethod").build(); assertThat(uriComponents.toUriString(), is("http://localhost/")); }
@Test public void fromMethodNamePathVariable() { UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, "methodWithPathVariable", "1").build(); assertThat(uriComponents.toUriString(), is("http://localhost/something/1/foo")); }
@Test public void fromMethodNameTwoPathVariables() { DateTime now = DateTime.now(); UriComponents uriComponents = fromMethodName( ControllerWithMethods.class, "methodWithTwoPathVariables", 1, now).build(); assertThat(uriComponents.getPath(), is("/something/1/foo/" + ISODateTimeFormat.date().print(now))); }
@Test // SPR-12977 public void fromMethodNameWithBridgedMethod() { UriComponents uriComponents = fromMethodName(PersonCrudController.class, "get", (long) 42).build(); assertThat(uriComponents.toUriString(), is("http://localhost/42")); }
@Test // SPR-11391 public void fromMethodNameTypeLevelPathVariableWithoutArgumentValue() { UriComponents uriComponents = fromMethodName(UserContactController.class, "showCreate", 123).build(); assertThat(uriComponents.getPath(), is("/user/123/contacts/create")); }
@Test // SPR-14405 public void fromMethodNameWithOptionalParam() { UriComponents uriComponents = fromMethodName(ControllerWithMethods.class, "methodWithOptionalParam", new Object[] {null}).build(); assertThat(uriComponents.toUriString(), is("http://localhost/something/optional-param")); }
@Test public void fromMethodNameWithMetaAnnotation() { UriComponents uriComponents = fromMethodName(MetaAnnotationController.class, "handleInput").build(); assertThat(uriComponents.toUriString(), is("http://localhost/input")); }
@Test // SPR-16710 public void fromMethodNameWithStringReturnType() { UriComponents uriComponents = fromMethodName( BookingControllerWithString.class, "getBooking", 21L).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@Test public void fromMethodNameWithPathVarAndRequestParam() { UriComponents uriComponents = fromMethodName( ControllerWithMethods.class, "methodForNextPage", "1", 10, 5).build(); assertThat(uriComponents.getPath(), is("/something/1/foo")); MultiValueMap<String, String> queryParams = uriComponents.getQueryParams(); assertThat(queryParams.get("limit"), contains("5")); assertThat(queryParams.get("offset"), contains("10")); }
@Test public void fromMethodNameTypeLevelPathVariable() { this.request.setContextPath("/myapp"); UriComponents uriComponents = fromMethodName( PersonsAddressesController.class, "getAddressesForCountry", "DE").buildAndExpand("1"); assertThat(uriComponents.toUriString(), is("http://localhost/myapp/people/1/addresses/DE")); }
@Test public void fromMethodNameWithCustomBaseUrlViaStaticCall() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); UriComponents uriComponents = fromMethodName(builder, ControllerWithMethods.class, "methodWithPathVariable", "1").build(); assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
/** * An alternative to {@link #fromMethodName(Class, String, Object...)}} for * use with an instance of this class created via {@link #relativeTo}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @since 4.2 */ public UriComponentsBuilder withMethodName(Class<?> controllerType, String methodName, Object... args) { return fromMethodName(this.baseUrl, controllerType, methodName, args); }
@GetMapping("/") public String listUploadedFiles(Model model) throws IOException { model.addAttribute("files", storageService.loadAll().map( path -> MvcUriComponentsBuilder.fromMethodName(FileUploadController.class, "serveFile", path.getFileName().toString()).build().toString()) .collect(Collectors.toList())); return "uploadForm"; }
@RequestMapping(value = "/files/list", method = RequestMethod.GET) public String listFiles(Model model) { List<Path> lodf = new ArrayList<>(); List<HRefModel> uris = new ArrayList<>(); try { lodf = storageService.listSourceFiles(storageService.getUploadLocation()); for(Path pt : lodf) { HRefModel href = new HRefModel(); href.setHref(MvcUriComponentsBuilder .fromMethodName(UploadController.class, "serveFile", pt.getFileName().toString()) .build() .toString()); href.setHrefText(pt.getFileName().toString()); uris.add(href); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } model.addAttribute("listOfEntries", uris); return "file_list :: urlFileList"; }
/** * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object, * com.thoughtworks.xstream.io.HierarchicalStreamWriter, * com.thoughtworks.xstream.converters.MarshallingContext) */ public void marshal( Object source, HierarchicalStreamWriter writer, MarshallingContext context) { @SuppressWarnings("unchecked") Collection<String> entries = (Collection<String>) source; for (String name : entries) { writer.startNode(alias); writer.startNode("name"); writer.setValue(name); writer.endNode(); // name writer.startNode("atom:link"); writer.addAttribute("xmlns:atom", "http://www.w3.org/2005/Atom"); writer.addAttribute("rel", "alternate"); UriComponents uriComponents = MvcUriComponentsBuilder.fromMethodName( controllerClass, alias + "Get", name) .buildAndExpand(""); writer.addAttribute( "href", uriComponents.encode().toUriString().replace("$", "") + ".xml"); writer.addAttribute("type", MediaType.TEXT_XML.toString()); writer.endNode(); writer.endNode(); } }