View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.rest.web;
    21
    22  import javax.ws.rs.GET;
    23  import javax.ws.rs.Path;
    24  import javax.ws.rs.core.Context;
    25  import javax.ws.rs.core.UriInfo;
    26  import java.io.FileInputStream;
    27  import java.io.IOException;
    28  import java.io.InputStream;
    29  import java.io.InputStreamReader;
    30  import java.io.Reader;
    31
    32  @Path("/resource")
    33  public class ResourcesService {
    34      final static String JAVASCRIPT_BODY;
    35      static {
    36          // FIXME This is so very ugly, it's because when running it with maven
    37          // it won't add the src/main/resources to the classpath
    38          String body = null;
    39          try {
    40              body = readResourceAsString("htmlbrowse.js");
    41          } catch (Exception e) {
    42              body = readFileAsString("src/main/resources/htmlbrowse.js");
    43          }
    44          JAVASCRIPT_BODY = body;
    45      }
    46
    47      public ResourcesService(@Context UriInfo uriInfo) {
    48      }
    49
    50      @GET
    51      @Path("htmlbrowse.js")
    52      public String getHtmlBrowseJavascript() {
    53          return JAVASCRIPT_BODY;
    54      }
    55
    56      private static String readFileAsString(String file) {
    57          try {
    58              return readAsString(new FileInputStream(file));
    59          } catch (IOException e) {
    60              throw new RuntimeException(e);
    61          }
    62      }
    63
    64      private static String readResourceAsString(String resource) {
    65          return readAsString(ClassLoader.getSystemResourceAsStream(resource));
    66      }
    67
    68      private static String readAsString(InputStream input) {
    69          final char[] buffer = new char[0x10000];
    70          StringBuilder out = new StringBuilder();
    71          Reader reader = null;
    72          try {
    73              reader = new InputStreamReader(input, "UTF-8" );
    74              int read;
    75              do {
    76                  read = reader.read(buffer, 0, buffer.length);
    77                  if (read > 0) {
    78                      out.append(buffer, 0, read);
    79                  }
    80              } while (read >= 0);
    81          } catch (IOException e) {
    82              throw new RuntimeException(e);
    83          } finally {
    84              if (reader != null) {
    85                  try {
    86                      reader.close();
    87                  } catch (IOException e) {
    88                      // OK
    89                  }
    90              }
    91          }
    92          return out.toString();
    93      }
    94  }