Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * *********************************************************************************************************************
   *
   * NorthernWind - lightweight CMS
   * http://northernwind.tidalwave.it - hg clone https://bitbucket.org/tidalwave/northernwind-src
   * %%
   * Copyright (C) 2011 - 2013 Tidalwave s.a.s. (http://tidalwave.it)
   * %%
  * *********************************************************************************************************************
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  * the License. You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations under the License.
  *
  * *********************************************************************************************************************
  *
  * $Id: AvailabilityEnforcerRequestProcessor.java,v 0f8d7eb32e63 2013/06/15 09:00:07 fabrizio $
  *
  * *********************************************************************************************************************
  * #L%
  */
 package it.tidalwave.northernwind.core.model.spi;
 
 import  javax.annotation.Nonnull;
 import  it.tidalwave.util.NotFoundException;
 import lombok.Getter;
 import lombok.Setter;
 import static org.springframework.core.Ordered.*;
 import static it.tidalwave.northernwind.core.model.RequestProcessor.Status.*;

This RequestProcessor returns an HTTP 503 status code, with a proper readable message, when the site is not available.

Author(s):
Fabrizio Giudici
Version:
$Id: AvailabilityEnforcerRequestProcessor.java,v 0f8d7eb32e63 2013/06/15 09:00:07 fabrizio $ /
 
   {
     @Inject @Nonnull
     private Provider<SiteProvidersiteProvider;
 
     @Getter @Setter // TODO: required for test, try to drop
     @Inject @Nonnull
     private ResponseHolder<?> responseHolder;
 
     @Override @Nonnull
     public Status process (final @Nonnull Request request)
       throws NotFoundException, IOExceptionHttpStatusException
       {
         if (.get().isSiteAvailable())
           {
             return ;
           }
 
         log.warn("Site unavailable, sending maintenance page");
         // TODO: use a resource
         final String page = String.format("<!DOCTYPE html>%n"
                 + "<html>\n"
                 + "<head>\n"
                 + "<meta http-equiv=\"Refresh\" content=\"15; url=%s%s\"/>\n"
                 + "</head>\n"
                 + "<body>\n"
                 + "<div style=\"padding: 5%% 0pt;\">\n"
                 + "<div style=\"padding: 10%% 0pt; text-align: center\">"
                 + "<p style=\"font-family: sans-serif; font-size: 24px\">"
                 + "Site under maintenance, please retry later.<br/>"
                 + "This page will reload automatically.</p>"
                 + "</div>\n"
                 + "</div>\n"
                 + "</body>\n"
                 + "</html>"request.getBaseUrl(), request.getOriginalRelativeUri());
         .response().withContentType("text/html")
                                  .withStatus(503)
                                  .withExpirationTime(new Duration(0))
                                  .withBody(page)
                                  .put();
         return ;
       }
  }
New to GrepCode? Check out our FAQ X