Java Code Examples for org.restlet.Request.setResourceRef()

The following are Jave code examples for showing how to use setResourceRef() of the org.restlet.Request class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: search   File: TestRestManager.java   Source Code and License Vote up 5 votes
@Test
public void testResolveResourceId () throws Exception {
  Request testRequest = new Request();
  Reference rootRef = new Reference("http://solr.apache.org/");
  testRequest.setRootRef(rootRef);

  Reference resourceRef = new Reference("http://solr.apache.org/schema/analysis/synonyms/de");
  testRequest.setResourceRef(resourceRef);

  String resourceId = RestManager.ManagedEndpoint.resolveResourceId(testRequest);
  assertEquals(resourceId, "/schema/analysis/synonyms/de");
}
 
Example 2
Project: search   File: TestRestManager.java   Source Code and License Vote up 5 votes
@Test
public void testResolveResourceIdDecodeUrlEntities () throws Exception {
  Request testRequest = new Request();
  Reference rootRef = new Reference("http://solr.apache.org/");
  testRequest.setRootRef(rootRef);

  Reference resourceRef = new Reference("http://solr.apache.org/schema/analysis/synonyms/de/%C3%84ndern");
  testRequest.setResourceRef(resourceRef);

  String resourceId = RestManager.ManagedEndpoint.resolveResourceId(testRequest);
  assertEquals(resourceId, "/schema/analysis/synonyms/de/Ändern");
}
 
Example 3
Project: DeviceConnect-Android   File: LocalOAuth2Main.java   Source Code and License Vote up 4 votes
/**
 * AuthorizationServerResourceを実行.
 * 
 * @param client クライアント情報
 * @param initialize 初期化フラグ(trueにするとContextを初期化する)
 * @param sessionId セッションID(引き継ぐセッションIDが存在すれば指定する、無ければnullを設定する)
 * @return not null: RedirectRepresentation型の戻り値を返す / null: エラー
 * @throws AuthorizationException Authorization例外.
 */
private static RedirectRepresentation callAuthorizationServerResource(final Client client,
        final boolean initialize, final String sessionId) throws AuthorizationException {

    // AuthorizationServerResourceを初期化する
    if (initialize) {
        Context context = new Context(sLogger);
        AuthorizationServerResource.init(context);
    }

    // request, responseを初期化 *
    Request request = new Request();
    request.setOriginalRef(new Reference(DUMMY_ORIGINAL_REF));
    Response response = new Response(request);
    request.setResourceRef(new Reference(DUMMY_REFERENCE));

    // セッションIDが指定されていたらRequestに設定する
    if (sessionId != null) {
        Series<Cookie> cookies = new Series<>(Cookie.class);
        cookies.add(AuthorizationBaseServerResource.ClientCookieID, sessionId);
        request.setCookies(cookies);
    }

    AuthorizationServerResource.init(request, response, sClientManager, sTokenManager);
    
    // Formに設定する
    Form paramsA = new Form();
    paramsA.add(AuthorizationServerResource.CLIENT_ID, client.getClientId());
    paramsA.add(AuthorizationServerResource.REDIR_URI, DUMMY_REDIRECT_URI);
    paramsA.add(AuthorizationServerResource.RESPONSE_TYPE, "code");
    paramsA.add(AuthorizationServerResource.SCOPE, DUMMY_SCOPE1);

    /// requestAuthorizationを実行する
    Representation representationA;
    try {
        representationA = AuthorizationServerResource.requestAuthorization(paramsA);
    } catch (OAuthException e) {
        throw new AuthorizationException(e);
    }

    // 正常終了(ログイン画面リダイレクト)
    if (representationA instanceof RedirectRepresentation) {
        return (RedirectRepresentation) representationA;
    }

    return null;
}
 
Example 4
Project: DeviceConnect-Android   File: LocalOAuth2Main.java   Source Code and License Vote up 4 votes
/**
 * LoginPageServerResourceを実行する.
 * 
 * @param userId ユーザーID
 * @param password パスワード
 * @return 戻り値(ResultRepresentation)
 */
private static ResultRepresentation callLoginPageServerResource(final String userId, final String password) {
    // 前の処理からセッションIDを引き継ぐ
    String sessionId = AuthorizationServerResource.getSessionId();
    Series<Cookie> cookies = new Series<>(Cookie.class);
    cookies.add(AuthorizationBaseServerResource.ClientCookieID, sessionId);

    // (B)の処理
    LoginPageServerResource.initResult();
    Request request = new Request();
    Reference requestReference = new Reference(DUMMY_ORIGINAL_REF);
    requestReference.addQueryParameter(LoginPageServerResource.USER_ID, userId);
    requestReference.addQueryParameter(LoginPageServerResource.PASSWORD, password);
    requestReference.addQueryParameter(LoginPageServerResource.CONTINUE,
            RedirectRepresentation.RedirectProc.requestAuthorization.toString());

    // QueryParameterとは別の変数に値を入れているので、直接値を設定する
    ArrayList<String> userIds = new ArrayList<>();
    userIds.add(userId);
    ArrayList<String> passwords = new ArrayList<>();
    passwords.add(password);
    ArrayList<String> continues = new ArrayList<>();
    continues.add(RedirectRepresentation.RedirectProc.requestAuthorization.toString());

    LoginPageServerResource.getQuery().put(LoginPageServerResource.USER_ID, userIds);
    LoginPageServerResource.getQuery().put(LoginPageServerResource.PASSWORD, passwords);
    LoginPageServerResource.getQuery().put(LoginPageServerResource.CONTINUE, continues);

    request.setCookies(cookies);
    request.setOriginalRef(requestReference);
    request.setResourceRef(requestReference);
    Response response = new Response(request);
    LoginPageServerResource.init(request, response);
    ResultRepresentation resultRepresentation;
    try {
        resultRepresentation = (ResultRepresentation) LoginPageServerResource.getPage();
    } catch (OAuthException e) {
        resultRepresentation = new ResultRepresentation();
        resultRepresentation.setResult(false);
        resultRepresentation.setError(e.getMessage(), e.getErrorDescription());
    }

    return resultRepresentation;
}
 
Example 5
Project: prudence   File: CachingUtil.java   Source Code and License Vote up 4 votes
/**
 * Casts the cache key template for an executable.
 * 
 * @param documentDescriptor
 *        The document descriptor
 * @param suffix
 *        The optional attribute suffix
 * @param parserName
 *        The parser to use, or null for the default parser
 * @param conversationService
 *        The conversation service
 * @param encoding
 *        The encoding
 * @return The cache key or null
 */
public String castKey( DocumentDescriptor<Executable> documentDescriptor, String suffix, String parserName, ResourceConversationServiceBase<R> conversationService, Encoding encoding )
{
	Executable executable = documentDescriptor.getDocument();
	String cacheKeyTemplate = getKeyTemplate( executable, suffix );
	if( cacheKeyTemplate == null )
		return null;

	Request request = resource.getRequest();
	Response response = resource.getResponse();

	if( !ProgramParser.NAME.equals( parserName ) )
	{
		// Set initial media type according to the document's tag (might
		// be used by resolver)
		if( conversationService.getMediaType() == null )
			conversationService.setMediaTypeExtension( documentDescriptor.getTag() );
	}

	// Template and its resolver
	Template template = new Template( cacheKeyTemplate );
	CachingKeyTemplateResolver<R> resolver = new CachingKeyTemplateResolver<R>( documentDescriptor, resource, conversationService, encoding, request, response );

	// Cache key template plugins
	callKeyTemplatePlugins( template, executable, suffix );

	Reference captiveReference = CapturingRedirector.getCapturedReference( request );
	Reference resourceReference = request.getResourceRef();

	try
	{
		// Temporarily use captive reference as the resource reference
		if( captiveReference != null )
			request.setResourceRef( captiveReference );

		// Cast it
		return template.format( resolver );
	}
	finally
	{
		// Return to original reference
		if( captiveReference != null )
			request.setResourceRef( resourceReference );
	}
}
 
Example 6
Project: prudence   File: ForwardedFilter.java   Source Code and License Vote up 4 votes
@Override
protected int beforeHandle( Request request, Response response )
{
	if( !isApplying() )
		return CONTINUE;

	String forwardedScheme = request.getHeaders().getFirstValue( X_FORWARDED_PROTO_HEADER );
	String forwardedDomain = request.getHeaders().getFirstValue( X_FORWARDED_HOST_HEADER );
	String forwardedPortString = request.getHeaders().getFirstValue( X_FORWARDED_PORT_HEADER );
	if( ( forwardedScheme != null ) || ( forwardedDomain != null ) || ( forwardedPortString != null ) )
	{
		int forwardedPort = -1;

		if( forwardedDomain != null )
		{
			int colon = forwardedDomain.indexOf( ':' );
			if( colon != -1 )
			{
				// Parse host:port
				try
				{
					forwardedPort = Integer.parseInt( forwardedDomain.substring( 0, colon ) );
				}
				catch( NumberFormatException x )
				{
					// Invalid
				}
				forwardedDomain = forwardedDomain.substring( 0, colon );
			}
		}

		if( forwardedPortString != null )
			// Note: will override port in X-Forwarded-Host
			forwardedPort = Integer.parseInt( forwardedPortString );

		Reference reference;

		reference = request.getResourceRef();
		if( reference != null )
		{
			apply( reference, forwardedScheme, forwardedDomain, forwardedPort );
			request.setResourceRef( reference );
		}

		reference = request.getRootRef();
		if( reference != null )
		{
			apply( reference, forwardedScheme, forwardedDomain, forwardedPort );
			request.setRootRef( reference );
		}

		reference = request.getOriginalRef();
		if( reference != null )
		{
			apply( reference, forwardedScheme, forwardedDomain, forwardedPort );
			request.setOriginalRef( reference );
		}
	}

	return CONTINUE;
}
 
Example 7
Project: prudence   File: ResolvingRedirector.java   Source Code and License Vote up 4 votes
@Override
protected void serverRedirect( Restlet next, Reference targetRef, Request request, Response response )
{
	validateNotRecursiveServerRedirect( targetRef, request, response );

	// This is essentially the original Restlet code modified to use
	// ResolvingTemplate.

	if( next == null )
		getLogger().warning( "No next Restlet provided for server redirection to " + targetRef );
	else
	{
		// Save the base URI if it exists as we might need it for
		// redirections
		Reference resourceRef = request.getResourceRef();
		Reference baseRef = resourceRef.getBaseRef();

		// Reset the protocol and let the dispatcher handle the protocol
		request.setProtocol( null );

		// Update the request to cleanly go to the target URI
		request.setResourceRef( targetRef );
		rewrite( request );
		next.handle( request, response );

		// Allow for response rewriting and clean the headers
		response.setEntity( rewrite( response.getEntity() ) );
		rewrite( response );
		request.setResourceRef( resourceRef );

		// In case of redirection, we may have to rewrite the redirect URI
		if( response.getLocationRef() != null )
		{
			Template rt = new ResolvingTemplate( this.targetTemplate );
			rt.setLogger( getLogger() );
			int matched = rt.parse( response.getLocationRef().toString(), request );

			if( matched > 0 )
			{
				String remainingPart = (String) request.getAttributes().get( "rr" );

				if( remainingPart != null )
					response.setLocationRef( baseRef.toString() + remainingPart );
			}
		}
	}
}