本文整理汇总了Java中com.talvish.tales.communication.Status类的典型用法代码示例。如果您正苦于以下问题:Java Status类的具体用法?Java Status怎么用?Java Status使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Status类属于com.talvish.tales.communication包,在下文中一共展示了Status类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: echo
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* An operation that can be run using an HTTP GET or HTTP POST.
* It requires a query string or post body parameter called 'value'
* and the value sent in will be echo back in the response.
* @param theValue
* @return
*/
@ResourceOperation( name="echo", path="GET | POST : echo")
public ResourceResult<String> echo(
@RequestParam( name="query_echo" )String theValue,
@HeaderParam( name="Origin" )String theOrigin,
@CookieParam(name = "cookie_echo") String theCookieValue ) {
Conditions.checkParameter( !Strings.isNullOrEmpty( theValue ), "query_echo", "need the parameter");
// the "cookie_echo" parameter doesn't have to be a string, it can be other types and
// it will get translated ... it can also be type Cookie, where you get the full
// servlet cookie type back to look at and do with as you please
ResourceResult<String> result = new ResourceResult<String>();
if( Strings.isNullOrEmpty( theCookieValue ) ) {
result.setResult( theValue, Status.OPERATION_COMPLETED );
} else {
result.setResult( theCookieValue, Status.OPERATION_COMPLETED );
}
return result;
}
示例2: hello
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* An HTTP GET operation that simple returns the string 'hello world'.
*/
@ResourceOperation( name="hello_world", path="GET : hello" )
public ResourceResult<String> hello( @HeaderParam( name="Origin" )String theOrigin ) {
ResourceResult<String> result = new ResourceResult<String>();
result.setResult( "hello world", Status.OPERATION_COMPLETED );
return result;
}
示例3: createUser
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* The HTTP request to create a new user in the system and showing an easy
* way to set the status to return on a successful call (operation created).
*/
@ResourceOperation( name="create_user", path="GET | POST : users/create", status=Status.OPERATION_CREATED ) // supporting GET just so a browser can easily be used for manual testing
public User createUser(
@RequestParam( name="user" )User theUser,
@HeaderParam( name="Authorization" )String theAuthToken ) {
Conditions.checkAuthorization( validateAuthorization( theAuthToken ), "Sample", "unauthorized attempt to access resource" );
Conditions.checkParameterNotNull( theUser, "user", "first name must be provided" );
Conditions.checkParameter( theUser.getId() == null, "user.id", "user id must be null" );
Conditions.checkParameter( !Strings.isNullOrEmpty( theUser.getFirstName() ), "user.first_name", "first name must be provided" );
return engine.createUser( theUser );
}
示例4: returnCustomSuccess
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* A custom success, in this case HTTP status code 201. The Status enum values map to HTTP status codes.
*/
@ResourceOperation( name="return_custom_success", path="GET : return_custom_success" )
public ResourceResult<String> returnCustomSuccess( @RequestParam( name="value" )String theValue ) {
ResourceResult<String> result = new ResourceResult<String>( );
result.setResult( theValue, Status.OPERATION_CREATED );
return result;
}
示例5: setHeaders
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* Sets headers in the result and the HTTP status code is a standard 200.
*/
@ResourceOperation( name="set_headers", path="GET : set_headers" )
public ResourceResult<Void> setHeaders( ) {
ResourceResult<Void> result = new ResourceResult<Void>( );
result.addHeader( "Custom-Header", "custom value" );
result.addHeader( "Accept-Language", "en-ca" );
result.setResult( Status.OPERATION_COMPLETED );
return result;
}
示例6: returnCustomFailure
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* A custom error response. The Status enum values map to HTTP status codes.
*/
@ResourceOperation( name="return_custom_failure", path="GET : return_custom_failure" )
public ResourceResult<String> returnCustomFailure( ) {
ResourceResult<String> result = new ResourceResult<String>( )
.setResult( Status.CALLER_UNAUTHORIZED, "custom_error", null )
.setMessage( "This call is unauthorized." );
return result;
}
示例7: doGet
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* This sample oonly handles get request.
*/
@Override
protected void doGet( HttpServletRequest theRequest, HttpServletResponse theResponse ) throws ServletException, IOException {
try {
// since this is a servlet, you and can do what you like and write back
// what you like, this is just to demonstrate options and helpers
ResponseHelper.writeSuccess( theRequest, theResponse, new JsonPrimitive( "Hello World" ) );
} catch( Exception e ) {
ResponseHelper.writeFailure( theRequest, theResponse, Status.LOCAL_ERROR, String.format( "An exception of type '%s' with message '%s' was thrown.", e.getClass().getSimpleName(), e.getMessage() ) ) ;
}
}
示例8: setResult
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* Sets the value and partial status information.
* @param theStatusCode the reason for the failure
* @return returns itself so that calls can be chained together
*/
public ResourceResult<V> setResult( V theValue, Status theStatusCode ) {
Preconditions.checkNotNull( theStatusCode, "need a status code" );
this.value = theValue;
this.code = theStatusCode;
this.subcode = null;
this.subject = null;
return this;
}
示例9: ResourceMethodResult
import com.talvish.tales.communication.Status; //导入依赖的package包/类
ResourceMethodResult( JsonElement theValue, Status theStatus ) {
Preconditions.checkNotNull( theValue, "need a value" );
this.value = theValue;
this.code = theStatus;
this.subcode = null;
this.subject = null;
this.message = null;
this.exception = null;
}
示例10: doCall
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* Private, shared, implementation that throws the 404 and records the failure.
*/
private void doCall( HttpServletRequest theRequest, HttpServletResponse theResponse ) {
String requestPath = theRequest.getRequestURL().toString();
logger.info( "Path '{}' is not mapped to a servlet or resource.", requestPath );
ResponseHelper.writeFailure(theRequest, theResponse, Status.CALLER_NOT_FOUND, FailureSubcodes.UNKNOWN_REQUEST, String.format( "Path '%s' is not mapped to a servlet or resource.", requestPath ), null );
httpInterface.recordBadUrl();
}
示例11: filterContract
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* This method will filter out requests that do not have proper contract versions.
* @param theHttpRequest the request being responded to
* @param theHttpResponse the response we can write out to
* @param theVersion the version of the contract being requested, which may be null/empty
* @return return true if filtered/handled, false otherwise
*/
protected boolean filterContract( HttpServletRequest theHttpRequest, HttpServletResponse theHttpResponse, String theVersion ) {
boolean filtered = false;
// NOTE: I would like to make the contract validation piece more sophisticated at some point
// where it could be managed by source location, something from the caller, certain
// contracts are more lax, etc ... even then it could be that a front-end could see
// the missing version and at that time bind to the latest known good, so the servlets
// still require it, but front-end picks latest
final boolean missingVersion = Strings.isNullOrEmpty( theVersion );
// here we want to check if we have a service request
// if we do, we want to grab the version parameter and validate
// that based on the contract we have the right version here
// NOTE: in the future we may actually need to locally route based on the version of the contract
if( missingVersion ) { // see if we have a version, but not the contract contract
ResponseHelper.writeFailure(
theHttpRequest,
theHttpResponse,
Status.CALLER_BAD_INPUT,
FailureSubcodes.VERSION_MISSING,
"version must be specified",
null );
logger.warn( "A service request was made on contract '{}' but a version wasn't specified.", getContract( ).getName() );
filtered = true;
} else if( !getContract( ).supports( theVersion ) ) { // see if the version is supported
ResponseHelper.writeFailure(
theHttpRequest,
theHttpResponse,
Status.CALLER_BAD_INPUT,
FailureSubcodes.VERSION_NOT_SUPPORTED,
String.format( "version '%s' is not supported", theVersion ),
null );
logger.warn( "A service request was made on contract '{}' asking for version '{}' which is not supported.", getContract( ).getName(), theVersion );
filtered = true;
}
return filtered;
}
示例12: _writeResponse
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* Shared helper method to write a failure response to the caller.
* @param theRequest the request object used
* @param theResponse The response object used to write back to the caller
* @param theFailure the type of failure seen
* @param theSubcode the code, specific to the caller, to return
* @param theMessage the message to indicate back to the caller
* @param theException the exception that indicates the the failure
*/
private static void _writeResponse( HttpServletRequest theRequest, HttpServletResponse theResponse, JsonElement theObject, Status theCode, String theSubcode, String theSubject, String theMessage, Throwable theException ) {
try {
;
Preconditions.checkNotNull( theResponse, "Need a response object." );
Preconditions.checkNotNull( theCode, "Need a status code." );
OperationContext operationContext = ( OperationContext )theRequest.getAttribute( AttributeConstants.OPERATION_REQUEST_CONTEXT );
// get the status code to use based on the error from the communication request
theResponse.setStatus( HttpStatus.convert( theCode ).getCode( ) );
setCommonHeaders( theResponse );
JsonObject bodyObject = new JsonObject( );
// add the main value/result to return
bodyObject.add( "return", theObject );
// now add all the operation related values
addResultMetadata( theRequest, operationContext, theCode, theSubcode, theSubject, theMessage, theException, bodyObject );
Gson targetGson = operationContext.getResponseTarget() == Readability.HUMAN ? humanGson: machineGson;
theResponse.getWriter().write( targetGson.toJson( bodyObject ) );
} catch( Exception e ) {
// if we cannot write back, then we have to log
// and we need to build up some form of alert and send as well
logger.warn(
String.format( "An error occurred while attempting to send a response of type '%s' with message '%s' to the caller.", theCode, theMessage ),
e );
}
// IF we have DEBUG turned on then we can
// send more over the wire
// so DEBUG option is something we record
// for the response back
// so we need categorization of the type of failures we want to
// to send and the information we are going to send with it
// document what it is
}
示例13: getTypes
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* Returns the list of id types in the system.
* @return the list of available IdTypes
*/
@ResourceOperation( name="get_id_types", path="GET : types" )
public ResourceResult<List<IdType>> getTypes( ) {
ResourceResult<List<IdType>> result = new ResourceResult<List<IdType>>( );
result.setResult( engine.getTypes(), Status.OPERATION_COMPLETED );
result.setCachingEnabled( engine.getMaximumCacheAge( ) );
return result;
}
示例14: getType
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* Returns a specific id type.
* @param theTypeId the type id to retrieve information for
* @return the information regarding the specified type id
*/
//@Cache( maxAge="com.somethign.something" )
//@NotNull( ) // though we would want it to act differently depending on issue (so, sometimes a 401 or so)
@ResourceOperation( name="get_id_type_by_id", path="GET : types/{type_id : [0-9]+}" ) // regex separates from below method, this must be a number
public ResourceResult<IdType> getType( @Min( 1 ) @PathParam( name="type_id" )int theTypeId ) {
ResourceResult<IdType> result = new ResourceResult<IdType>( );
IdType idType = engine.getType( theTypeId );
Conditions.checkFound( idType != null, Integer.toString( theTypeId ), "Could not find the type identified by the id '%s'.", theTypeId );
result.setResult( idType, Status.OPERATION_COMPLETED );;
result.setCachingEnabled( engine.getMaximumCacheAge( ) );
return result;
}
示例15: doCall
import com.talvish.tales.communication.Status; //导入依赖的package包/类
/**
* Private, shared, implementation of the method used to shutdown the service.
*/
private void doCall( HttpServletRequest theRequest, HttpServletResponse theResponse ) {
String operation = theRequest.getPathInfo( );
if( Strings.isNullOrEmpty( operation ) ) {
ResponseHelper.writeFailure(
theRequest,
theResponse,
Status.CALLER_BAD_INPUT,
FailureSubcodes.UNKNOWN_REQUEST,
"Missing the operation.",
null );
} else {
// if( theRequest.getParameterMap().size() > 1 ) {
// HttpService.writeFailure(
// theRequest,
// theResponse,
// Failure.CALLER_BAD_DATA,
// "0 or 1 parameters are expected.",
// null );
// } else {
if( operation.equals( STOP ) ) {
close( );
ResponseHelper.writeSuccess( theRequest, theResponse );
} else if( operation.equals( KILL ) ) {
ResponseHelper.writeSuccess( theRequest, theResponse );
kill( );
} else if( operation.equals( SUSPEND ) ) {
// String period = theRequest.getParameter( "period" );
// Integer integerPeriod = null;
// DateTime dateTimePeriod = null;
//
// if( !Strings.isNullOrEmpty( period ) ) {
// try {
// integerPeriod = ( Integer )integerTranslator.translate( period );
// dateTimePeriod = ( DateTime )dateTimeTranslator.translate( period );
// } catch( TranslationException e) {
//
// }
// }
if( suspend( ) ) {
ResponseHelper.writeSuccess( theRequest, theResponse );
} else {
ResponseHelper.writeFailure(
theRequest,
theResponse,
Status.CALLER_BAD_STATE,
ALREADY_SUSPENED,
"Not in a state to be able to suspend.",
null );
}
} else if( operation.equals( RESUME ) ) {
if( resume( ) ) {
ResponseHelper.writeSuccess( theRequest, theResponse );
} else {
ResponseHelper.writeFailure(
theRequest,
theResponse,
Status.CALLER_BAD_STATE,
ALREADY_RUNNING,
"Not in a state to be able to resume.",
null );
}
} else {
logger.info( "Unknown operation '{}'.", operation );
ResponseHelper.writeFailure(
theRequest,
theResponse,
Status.CALLER_BAD_INPUT,
FailureSubcodes.UNKNOWN_REQUEST,
String.format( "The operation '%s' is not valid.", operation ),
null );
}
}
}