Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
     Copyright (c) 2012 LinkedIn Corp.
  
     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.linkedin.restli.docgen;
 
 import java.net.URI;
 import java.util.List;
 import java.util.Map;

Default com.linkedin.restli.server.RestLiDocumentationRequestHandler that serves both HTML and JSON documentation.

Author(s):
Keren Jin
 
 {
   @Override
   public void initialize(RestLiConfig configMap<StringResourceModelrootResources)
   {
     final DataSchemaResolver schemaResolver = new ClassNameDataSchemaResolver();
     final ResourceSchemaCollection resourceSchemas = ResourceSchemaCollection.loadOrCreateResourceSchema(rootResources);
     final RestLiResourceRelationship relationships = new RestLiResourceRelationship(resourceSchemasschemaResolver);
 
                                                         relationships,
                                                         rootResources,
                                                         new VelocityTemplatingEngine(),
                                                         schemaResolver);
      = new RestLiJSONDocumentationRenderer(relationships);
   }
 
   @Override
   public boolean isDocumentationRequest(RestRequest request)
   {
     return request.getURI().getRawPath().startsWith("/" + ) || HttpMethod.valueOf(request.getMethod()) == .;
   }
 
   @Override
   @SuppressWarnings("fallthrough")
   {
     final String path = request.getURI().getRawPath();
     final List<UriComponent.PathSegmentpathSegments = UriComponent.decodePath(pathtrue);
 
     String prefixSegment = null;
     String actionSegment = null;
     String typeSegment = null;
     String objectSegment = null;
 
     switch (pathSegments.size())
     {
       case 5:
         objectSegment = pathSegments.get(4).getPath();
       case 4:
         typeSegment = pathSegments.get(3).getPath();
       case 3:
         actionSegment = pathSegments.get(2).getPath();
       case 2:
         prefixSegment = pathSegments.get(1).getPath();
     }
 
     assert(prefixSegment.equals());
 
     final RestLiDocumentationRenderer renderer;
 
     if (HttpMethod.valueOf(request.getMethod()) == .)
     {
       renderer = ;
       renderer.renderResource(prefixSegmentout);
    }
    else if (HttpMethod.valueOf(request.getMethod()) == .)
    {
      if (!.equals(actionSegment))
      {
        throw createRoutingError(path);
      }
      final MultivaluedMap queryMap = UriComponent.decodeQuery(request.getURI().getQuery(), false);
      final List<StringformatList = queryMap.get("format");
      if (formatList == null)
      {
        renderer = ;
      }
      else if (formatList.size() > 1)
      {
        throw new RoutingException(
            String.format("\"format\" query parameter must be unique, where multiple are specified: %s",
            Arrays.toString(formatList.toArray())),
            ..getCode());
      }
      else
      {
        renderer = (formatList.contains() ?  : );
      }
      if (renderer == )
      {
        .setJsonFormatUri(UriBuilder.fromUri(request.getURI())
                                                 .queryParam("format")
                                                 .build());
      }
      try
      {
        if (typeSegment == null || typeSegment.isEmpty())
        {
          renderer.renderHome(out);
        }
        else
        {
          if (.equals(typeSegment))
          {
            if (objectSegment == null || objectSegment.isEmpty())
            {
              renderer.renderResourceHome(out);
            }
            else
            {
              renderer.renderResource(objectSegmentout);
            }
          }
          else if (.equals(typeSegment))
          {
            if (objectSegment == null || objectSegment.isEmpty())
            {
              renderer.renderDataModelHome(out);
            }
            else
            {
              renderer.renderDataModel(objectSegmentout);
            }
          }
          else
          {
            throw createRoutingError(path);
          }
        }
      }
      catch (RuntimeException e)
      {
        if (!renderer.handleException(eout))
        {
          throw e;
        }
      }
    }
    else
    {
    }
    return new RestResponseBuilder().
           setStatus(..getCode()).
           setHeader(renderer.getMIMEType()).
           setEntity(out.toByteArray()).
           build();
  }
  private static RoutingException createRoutingError(String path)
  {
    return new RoutingException(String.format("Invalid documentation path %s"path), ..getCode());
  }
  private static final String DOC_PREFIX = "restli";
  private static final String DOC_VIEW_DOCS_ACTION = "docs";
  private static final String DOC_RESOURCE_TYPE = "rest";
  private static final String DOC_DATA_TYPE = "data";
  private static final String DOC_JSON_FORMAT = "json";
  private static final String HTTP_CONTENT_TYPE_HEADER = "Content-Type";
  private static final int BAOS_BUFFER_SIZE = 8192;
New to GrepCode? Check out our FAQ X