001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest; 014 015import java.text.*; 016import java.util.logging.*; 017 018import javax.servlet.http.*; 019 020import org.apache.juneau.dto.swagger.*; 021import org.apache.juneau.html.annotation.*; 022import org.apache.juneau.internal.*; 023import org.apache.juneau.rest.annotation.*; 024import org.apache.juneau.http.exception.*; 025 026/** 027 * Identical to {@link BasicRestServlet} but doesn't extend from {@link HttpServlet}. 028 * 029 * <ul class='seealso'> 030 * <li class='link'>{@doc juneau-rest-server.Instantiation.BasicRest} 031 * </ul> 032 */ 033@Rest( 034 // Allow OPTIONS requests to be simulated using ?method=OPTIONS query parameter. 035 allowedMethodParams="OPTIONS" 036) 037@HtmlDocConfig( 038 // Basic page navigation links. 039 navlinks={ 040 "up: request:/..", 041 "options: servlet:/?method=OPTIONS" 042 } 043) 044public abstract class BasicRest implements BasicRestConfig { 045 046 private JuneauLogger logger = JuneauLogger.getLogger(getClass()); 047 private volatile RestContext context; 048 049 /** 050 * Post-initialization hook to retrieve the {@link RestContext} object for this resource. 051 * 052 * @param context The context for this resource. 053 */ 054 @RestHook(HookEvent.POST_INIT) 055 public synchronized void onPostInit(RestContext context) { 056 this.context = context; 057 } 058 059 /** 060 * [OPTIONS /*] - Show resource options. 061 * 062 * @param req The HTTP request. 063 * @return A bean containing the contents for the OPTIONS page. 064 */ 065 @Override /* BasicRestConfig */ 066 public Swagger getOptions(RestRequest req) { 067 // Localized Swagger for this resource is available through the RestRequest object. 068 return req.getSwagger(); 069 } 070 071 //----------------------------------------------------------------------------------------------------------------- 072 // Context methods. 073 //----------------------------------------------------------------------------------------------------------------- 074 075 /** 076 * Returns the read-only context object that contains all the configuration information about this resource. 077 * 078 * @return The context information on this servlet. 079 */ 080 protected synchronized RestContext getContext() { 081 if (context == null) 082 throw new InternalServerError("RestContext object not set on resource."); 083 return context; 084 } 085 086 //----------------------------------------------------------------------------------------------------------------- 087 // Convenience logger methods 088 //----------------------------------------------------------------------------------------------------------------- 089 090 /** 091 * Log a message. 092 * 093 * @param msg The message to log. 094 */ 095 public void log(String msg) { 096 logger.info(msg); 097 } 098 099 /** 100 * Log a message. 101 * 102 * @param msg The message to log. 103 * @param cause The cause. 104 */ 105 public void log(String msg, Throwable cause) { 106 logger.info(cause, msg); 107 } 108 109 /** 110 * Log a message. 111 * 112 * @param level The log level. 113 * @param msg The message to log. 114 * @param args Optional {@link MessageFormat}-style arguments. 115 */ 116 public void log(Level level, String msg, Object...args) { 117 logger.log(level, msg, args); 118 } 119 120 /** 121 * Log a message. 122 * 123 * @param level The log level. 124 * @param msg The message to log. 125 * @param args Optional {@link MessageFormat}-style arguments. 126 */ 127 public void logObjects(Level level, String msg, Object...args) { 128 logger.logObjects(level, msg, args); 129 } 130 131 /** 132 * Log a message. 133 * 134 * @param level The log level. 135 * @param cause The cause. 136 * @param msg The message to log. 137 * @param args Optional {@link MessageFormat}-style arguments. 138 */ 139 public void log(Level level, Throwable cause, String msg, Object...args) { 140 logger.log(level, cause, msg, args); 141 } 142 143 //----------------------------------------------------------------------------------------------------------------- 144 // Request-time methods. 145 //----------------------------------------------------------------------------------------------------------------- 146 147 /** 148 * Returns the current HTTP request. 149 * 150 * @return The current HTTP request. 151 */ 152 public synchronized RestRequest getRequest() { 153 return getContext().getRequest(); 154 } 155 156 /** 157 * Returns the current HTTP response. 158 * 159 * @return The current HTTP response 160 */ 161 public synchronized RestResponse getResponse() { 162 return getContext().getResponse(); 163 } 164}