* <p>Write the content of the TAP service's home page.</p>
*
* <p><i>Note:
* This class is using the two following {@link TAP} attributes in order to display the home page:
* {@link TAP#homePageURI} and {@link TAP#homePageMimeType}. The MIME type is used only for the third case below (local file).
* </i></p>
*
* <p>Four cases are taken into account in this class, in function of the {@link TAP#homePageURI} value:</p>
* <ol>
* <li><b>a default content</b> if no custom home page (URI) has been specified using {@link TAP#setHomePageURI(String)}.
* This default home page is hard-coded in this class and displays just an HTML list of
* links. There is one link for each resources of this TAP service (excluding the home page).</li>
* <li><b>a file inside WebContent</b> if the given URI has no scheme (e.g. "tapIndex.jsp" or "/myFiles/tapIndex.html").
* The URI is then an absolute (if starting with "/") or a relative path to file inside the WebContent directory.
* In this case the request is forwarded to this file. It is neither a redirection nor a copy,
* but a kind of inclusion of the interpreted file into the response.
* <i>This method MUST be used if your home page is a JSP.</i></li>
* <li><b>a local file</b> if a URI starts with "file:". In this case, the content of the local file is copied in the HTTP response. There is no interpretation. So this method should not be used for JSP.</li>
* <li><b>a distance document</b> in all other cases. Indeed, if there is a scheme different from "file:" the given URI will be considered as a URL.
* In this case, any request to the TAP home page is redirected to this URL.</li>
* </ol>
*
* @author Grégory Mantelet (ARI)
* @version 2.0 (02/2015)
* @since 2.0
*/
publicclassHomePageimplementsTAPResource{
/** Name of this TAP resource. */
publicstaticfinalStringRESOURCE_NAME="HOME PAGE";
/** TAP service owning this resource. */
protectedfinalTAPtap;
publicHomePage(finalTAPtap){
if(tap==null)
thrownewNullPointerException("Missing TAP object! The HOME PAGE resource can not be initialized without a TAP instance.");
tap.getLogger().logTAP(LogLevel.ERROR,null,"HOME_PAGE","Can not write the specified home page content ("+tap.homePageURI+"): Web application file not found!",null);
// Copy the content of the input into the given writer:
byte[]buffer=newbyte[2048];
intnbReads=0;
while((nbReads=input.read(buffer))>0)
writer.print(newString(buffer,0,nbReads));
// copy successful:
written=true;
}else
tap.getLogger().logTAP(LogLevel.ERROR,null,"HOME_PAGE","Can not write the specified home page content ("+tap.homePageURI+"): File not found or not readable ("+f.exists()+!f.isDirectory()+f.canRead()+")!",null);
}finally{
if(input!=null)
input.close();
}
}
/* CASE: HTTP/HTTPS/FTP/... */
else{
response.sendRedirect(tap.homePageURI);
written=true;
}
}catch(Exceptione){
tap.getLogger().logTAP(LogLevel.ERROR,null,"HOME_PAGE","Can not write the specified home page content ("+tap.homePageURI+"): "+e.getMessage(),e);
}
}
// DEFAULT: list all available resources:
if(!written){
// Set the content type: HTML document
response.setContentType("text/html");
// Write the home page:
writer.println("<html><head><title>TAP HOME PAGE</title></head><body><h1 style=\"text-align: center\">TAP HOME PAGE</h1><h2>Available resources:</h2><ul>");