Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com 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.
 
 package com.helger.webbasics.ajax.response;
 
 
 
 
 {
  
Success property
 
   public static final String PROPERTY_SUCCESS = "success";
  
Response value property - only in case of success - contains the response data as object
 
   public static final String PROPERTY_VALUE = "value";
  
Additional CSS files - only in case of success - contains a list of strings
 
   public static final String PROPERTY_EXTERNAL_CSS = "externalcss";
  
Additional inline CSS - only in case of success - contains a string
 
   public static final String PROPERTY_INLINE_CSS = "inlinecss";
  
Additional JS files - only in case of success - contains a list of strings
 
   public static final String PROPERTY_EXTERNAL_JS = "externaljs";
  
Additional inline JS - only in case of success - contains a string
 
   public static final String PROPERTY_INLINE_JS = "inlinejs";
  
Error message property - only in case of error
 
   public static final String PROPERTY_ERRORMESSAGE = "errormessage";
  
Default property for HTML content
 
   public static final String PROPERTY_HTML = "html";
 
   private static final ReadWriteLock s_aRWLock = new ReentrantReadWriteLock ();

  
The converter to be used from relative URIs (for CSS and JS) to absolute URLs.
 
   @GuardedBy ("s_aRWLock")
   private static IWebURIToURLConverter s_aConverter = StreamURIToURLConverter.getInstance ();
 
   private final boolean m_bSuccess;
   private final String m_sErrorMessage;
   private final IJson m_aSuccessValue;
 
   private void _addCSSAndJS (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                              @Nonnull final IWebURIToURLConverter aConverter)
   {
     ValueEnforcer.notNull (aRequestScope"RequestScope");
     ValueEnforcer.notNull (aConverter"URIToURLConverter");
 
     // Grab per-request CSS/JS only in success case!
     // Grab all CSS/JS independent of conditional comment :(
     final boolean bRegularFiles = GlobalDebug.isDebugMode ();
 
     for (final ISimpleURL aCSSPath : PerRequestCSSIncludes.getAllRegisteredCSSIncludeURLsForThisRequest (aRequestScope,
                                                                                                          aConverter,
                                                                                                          bRegularFiles))
      addExternalCSS (aCSSPath.getAsString ());
    for (final ISimpleURL aJSPath : PerRequestJSIncludes.getAllRegisteredJSIncludeURLsForThisRequest (aRequestScope,
                                                                                                      aConverter,
                                                                                                      bRegularFiles))
      addExternalJS (aJSPath.getAsString ());
  }

  
Success constructor for HC nodes

Parameters:
aNode The response HTML node. May be null.
  protected AjaxDefaultResponse (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                 @Nullable final IHCNode aNode,
                                 @Nonnull final IWebURIToURLConverter aConverter)
  {
    // Do it first
    _addCSSAndJS (aRequestScopeaConverter);
    // Now decompose the HCNode itself
    final JsonObject aObj = new JsonObject ();
    if (aNode != null)
    {
      // Customize before extracting special content
      if (aNode instanceof IHCNodeWithChildren <?>)
        HCUtils.customizeNodes ((IHCNodeWithChildren <?>) aNode, HCSettings.getConversionSettings ());
      IHCNode aRealNode;
      if (aNode instanceof IHCHasChildren)
      {
        // no need to keepOnDocumentReady stuff as the document is already
        // loaded
        final boolean bKeepOnDocumentReady = false;
        aRealNode = HCSpecialNodeHandler.extractSpecialContent ((IHCHasChildrenaNodethisbKeepOnDocumentReady);
      }
      else
        aRealNode = aNode;
      // Serialize remaining node to HTML
      aObj.add (, HCSettings.getAsHTMLStringWithoutNamespaces (aRealNode));
    }
     = true;
     = null;
     = aObj;
  }
  protected AjaxDefaultResponse (final boolean bSuccess,
                                 @Nullable final String sErrorMessage,
                                 @Nullable final IJson aSuccessValue,
                                 @Nullable final IRequestWebScopeWithoutResponse aRequestScope,
                                 @Nullable final IWebURIToURLConverter aConverter)
  {
     = bSuccess;
     = sErrorMessage;
     = aSuccessValue;
    if (bSuccess)
      _addCSSAndJS (aRequestScopeaConverter);
  }
  public static void setDefaultURIToURLConverter (@Nonnull final IWebURIToURLConverter aConverter)
  {
    ValueEnforcer.notNull (aConverter"Converter");
    .writeLock ().lock ();
    try
    {
       = aConverter;
    }
    finally
    {
      .writeLock ().unlock ();
    }
  }
  {
    .readLock ().lock ();
    try
    {
      return ;
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public boolean isSuccess ()
  {
    return ;
  }
  public boolean isFailure ()
  {
    return !;
  }

  

Returns:
In case this is a failure, this field contains the error message. May be null.
  public String getErrorMessage ()
  {
    return ;
  }

  

Returns:
In case this is a success, this field contains the success object. May be null.
  public IJson getSuccessValue ()
  {
    return ;
  }
  public String getSerializedAsJSON (final boolean bIndentAndAlign)
  {
    final JsonObject aAssocArray = new JsonObject ();
    aAssocArray.add ();
    if ()
    {
      if ( != null)
        aAssocArray.add ();
      if (hasExternalCSSs ())
        aAssocArray.add (getAllExternalCSSs ());
      if (hasInlineCSS ())
        aAssocArray.add (getInlineCSS ());
      if (hasExternalJSs ())
        aAssocArray.add (getAllExternalJSs ());
      if (hasInlineJS ())
        aAssocArray.add (getInlineJS ().getJSCode ());
    }
    else
    {
      aAssocArray.add ( != null ?  : "");
    }
    return JsonWriter.getAsString (aAssocArray);
  }
  public boolean equals (final Object o)
  {
    if (o == this)
      return true;
    if (!super.equals (o))
      return false;
    final AjaxDefaultResponse rhs = (AjaxDefaultResponseo;
    return  == rhs.m_bSuccess &&
           EqualsUtils.equals (rhs.m_sErrorMessage) &&
           EqualsUtils.equals (rhs.m_aSuccessValue);
  }
  public int hashCode ()
  {
    return HashCodeGenerator.getDerived (super.hashCode ())
                            .append ()
                            .append ()
                            .append ()
                            .getHashCode ();
  }
  public String toString ()
  {
    return ToStringGenerator.getDerived (super.toString ())
                            .append ("success")
                            .appendIfNotNull ("errorMsg")
                            .appendIfNotNull ("successValue")
                            .toString ();
  }
  public static AjaxDefaultResponse createSuccess (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope)
  {
    return createSuccess (aRequestScopegetDefaultURIToURLConverter ());
  }
  public static AjaxDefaultResponse createSuccess (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                                   @Nonnull final IWebURIToURLConverter aConverter)
  {
    return createSuccess (aRequestScope, (IJsonnullaConverter);
  }
  public static AjaxDefaultResponse createSuccess (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                                   @Nullable final IJson aSuccessValue)
  {
    return createSuccess (aRequestScopeaSuccessValuegetDefaultURIToURLConverter ());
  }
  public static AjaxDefaultResponse createSuccess (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                                   @Nullable final IJson aSuccessValue,
                                                   @Nonnull final IWebURIToURLConverter aConverter)
  {
    return new AjaxDefaultResponse (truenullaSuccessValueaRequestScopeaConverter);
  }
  public static AjaxDefaultResponse createSuccess (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                                   @Nullable final IHCNode aNode)
  {
    // Use the default converter here
    return createSuccess (aRequestScopeaNodegetDefaultURIToURLConverter ());
  }
  public static AjaxDefaultResponse createSuccess (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                                   @Nullable final IHCNode aNode,
                                                   @Nonnull final IWebURIToURLConverter aConverter)
  {
    // Special case required
    return new AjaxDefaultResponse (aRequestScopeaNodeaConverter);
  }
  public static AjaxDefaultResponse createError ()
  {
    return createError ((Stringnull);
  }
  public static AjaxDefaultResponse createError (@Nullable final String sErrorMessage)
  {
    // No request scope needed in case of error!
    // No converter needed in case of error!
    return new AjaxDefaultResponse (false,
                                    sErrorMessage,
                                    (IJsonnull,
                                    (IRequestWebScopeWithoutResponsenull,
                                    (IWebURIToURLConverternull);
  }
New to GrepCode? Check out our FAQ X