@Override public Configuration getConfiguration(final ServletContext context) { Configuration config = ConfigurationBuilder.begin() .addRule() .when(Direction.isInbound().and(Path.matches("/{path}"))) .perform(Forward.to("/{path}/{path}")) .addRule() .when(Direction.isInbound().and(Path.matches("/success/success"))) .perform(SendStatus.code(200)); return config; }
@Override public Configuration getConfiguration(final ServletContext context) { return ConfigurationBuilder.begin() // one single transformer .addRule() .when(Path.matches("{something}.txt").where("something").matches(".*")) .perform(Transform.with(UppercaseTransformer.class)); }
@Override public Configuration getConfiguration(final ServletContext context) { return ConfigurationBuilder.begin() .addRule() .when( Direction.isInbound().and(Path.matches("/forward")).and(RequestParameter.exists("foo"))) .perform(Forward.to("/forward2?baz=cab")) .addRule() .when( Direction.isInbound() .and(Path.matches("/forward2")) .and(RequestParameter.exists("foo")) .and(RequestParameter.exists("baz"))) .perform(SendStatus.code(200)) .addRule() .when( Direction.isInbound() .and(Path.matches("/forward-fail")) .and(RequestParameter.exists("foo"))) .perform(Forward.to("/forward2")); }
/** * Uses the ReWrtie ConfigurationBuilder to define a set of rules used for the URL rewriting. We * define a single rule that rewrites requests for the index page if the request is determined to * originate from a mobile browser. * * @param context the ServletContext * @return the ReWrite Configuration */ @Override public Configuration getConfiguration(final ServletContext context) { return ConfigurationBuilder.begin() .defineRule() .when( Direction.isInbound() .and(Path.matches("/").or(Path.matches("/index.jsf"))) .and( new HttpCondition() { @Override public boolean evaluateHttp( HttpServletRewrite httpServletRewrite, EvaluationContext evaluationContext) { HttpServletRequest request = httpServletRewrite.getRequest(); String userAgentStr = request.getHeader("user-agent"); String httpAccept = request.getHeader("Accept"); UAgentInfo uAgentInfo = new UAgentInfo(userAgentStr, httpAccept); return uAgentInfo.detectTierIphone() || uAgentInfo.detectTierTablet(); } })) .perform(Forward.to("/secure/mobile/addSale.jsf")); }
@Override public Configuration getConfiguration(final ServletContext context) { Configuration config = ConfigurationBuilder.begin() .addRule(Join.path("/metadata").to("/internalMetadata")) .addRule() .perform(Log.message(Level.INFO, "Just loggin somethin'.")) .addRule() .when(Path.matches("/internalMetadata")) .perform( new Operation() { @Override public void perform(Rewrite event, EvaluationContext context) { throw new IllegalStateException("expected"); } }); return config; }
/* (non-Javadoc) * @see org.ocpsoft.rewrite.config.ConfigurationProvider#getConfiguration(java.lang.Object) */ @Override public Configuration getConfiguration(ServletContext context) { log.debug("Preparing legacy Person rewrite configuration"); final ConfigurationBuilder builder = ConfigurationBuilder.begin(); for (Map.Entry<String, TenantState> entry : tenantRepo.getStates().entrySet()) { if (entry.getValue() == TenantState.ACTIVE) { final String tenantId = entry.getKey(); final PersonRepository personRepo = personRepos.get(tenantId); final Set<String> slugPaths = personRepo.findAllSlugsByStatus(StatusMask.ACTIVE_ONLY); if (!slugPaths.isEmpty()) { log.debug("{}» Rewriting {} old Person URIs", tenantId, slugPaths.size()); for (String slugPath : slugPaths) { builder .addRule() .when(Direction.isInbound().and(Path.matches("/" + slugPath))) .perform( Redirect.permanent( "/" + SeoBookmarkableMapper.DEFAULT_LOCALE_PREF_ID + "/" + slugPath)); } } } } return builder; }
@Override public Configuration getConfiguration(final ServletContext context) { return ConfigurationBuilder.begin() .defineRule() /** * Define the inbound conditions and conversion mechanisms to be used when handling inbound * requests. */ .when( Method.isGet() .and( Path.matches("/store/product/{pid}") .where("pid") .matches("\\d+") .constrainedBy( new Constraint<String>() { @Override public boolean isSatisfiedBy( Rewrite event, EvaluationContext context, String value) { Integer valueOf = Integer.valueOf(value); return false; } }) .bindsTo( Evaluation.property("pid") .convertedBy(ProductConverter.class) .validatedBy(ProductValidator.class)))) .perform( new HttpOperation() { @Override public void performHttp( final HttpServletRewrite event, final EvaluationContext context) { /** * Extract the stored {pid} from our Path and load the Product. This is an example * of how we can use a converter to directly bind and store the object we want into * a binding. {@link Evaluation} is an low-level construct, and binds array values * that must be dereferenced. If using other bindings such as {@link El}, the value * will be bound directly to the type of the referenced property type, and this * array downcast is not necessary. */ Product product = (Product) Evaluation.property("pid").retrieve(event, context); /** * Marshal the Product into XML using JAXB. This has been extracted into a utility * class. */ try { XMLUtil.streamFromObject( Product.class, product, event.getResponse().getOutputStream()); } catch (IOException e) { throw new RuntimeException(e); } /** * Set the content type and status code of the response, this again could be * extracted into a REST utility class. */ event.getResponse().setContentType("application/xml"); ((HttpInboundServletRewrite) event).sendStatusCode(200); } }) .defineRule() .when(Path.matches("/store/products").and(Method.isGet())) .perform( new HttpOperation() { @Override public void performHttp( final HttpServletRewrite event, final EvaluationContext context) { try { XMLUtil.streamFromObject( ProductRegistry.class, products, event.getResponse().getOutputStream()); event.getResponse().setContentType("application/xml"); ((HttpInboundServletRewrite) event).sendStatusCode(200); } catch (Exception e) { throw new RuntimeException(e); } } }) .defineRule() .when(Path.matches("/store/products").and(Method.isPost())) .perform( new HttpOperation() { @Override public void performHttp( final HttpServletRewrite event, final EvaluationContext context) { try { Product product = XMLUtil.streamToObject(Product.class, event.getRequest().getInputStream()); product = products.add(product); /** * Just for fun, set a response header containing the URL to the newly created * Product. */ String location = new ParameterizedPattern(event.getContextPath() + "/store/product/{pid}") .build(product.getId()); Response.addHeader("Location", location).perform(event, context); event.getResponse().setContentType("text/html"); ((HttpInboundServletRewrite) event).sendStatusCode(200); } catch (Exception e) { throw new RuntimeException(e); } } }) .defineRule() .when(Path.matches("/")) .perform( new HttpOperation() { @Override public void performHttp( final HttpServletRewrite event, final EvaluationContext context) { try { PrintWriter writer = event.getResponse().getWriter(); writer.write( "<html>" + "<body>" + "<h1>Rewrite Rest Demo</h1>" + "Sorry for the boring page, there are no HTML pages in this demo! Try some of the following operations:" + "" + "<ul>" + "<li>GET <a href=\"" + event.getContextPath() + "/store/product/0\">/store/product/0</a></li>" + "<li>GET <a href=\"" + event.getContextPath() + "/store/products\">/store/products</a></li>" + "<li>POST " + event.getContextPath() + "/store/products - This requires a rest client, or you can use `curl`<br/>" + "curl --data \"<product><name>James</name><description>yay</description><price>12.9</price></product>\" http://localhost:8080/rewrite-showcase-rest/store/products</li>" + "</ul>" + "</body></html>"); SendStatus.code(200).perform(event, context); } catch (IOException e) { throw new RuntimeException(); } } }); }
public static IPath captureIn(final String param) { Path path = new Path("{" + param + "}"); path.where(param).matches(".*"); return path; }