Java Code Examples for org.restlet.util.Series#add()

The following examples show how to use org.restlet.util.Series#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoteCarService.java    From microservices-comparison with Apache License 2.0 6 votes vote down vote up
@Override
public List<Car> list() {
    Client client = new Client(new Context(), Protocol.HTTPS);
    Series<Parameter> parameters = client.getContext().getParameters();
    parameters.add("truststorePath", System.getProperty("javax.net.ssl.trustStore"));

    ClientResource clientResource = new ClientResource("https://localhost:8043/api/cars/cars");
    clientResource.setNext(client);
    ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER);
    challenge.setRawValue(Request.getCurrent().getAttributes().getOrDefault("token", "").toString());
    clientResource.setChallengeResponse(challenge);
    CarServiceInterface carServiceInterface = clientResource.wrap(CarServiceInterface.class);
    Car[] allCars = carServiceInterface.getAllCars();
    try {
        client.stop();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return asList(allCars);
}
 
Example 2
Source File: LocalOAuth2Main.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * AuthPageServerResourceを実行する.
 * 
 * @param sessionId セッションID
 * @param scopes スコープ
 * @param applicationName アプリケーション名
 * @return 戻り値(RedirectRepresentation)
 */
private RedirectRepresentation callAuthPageServerResource(final String sessionId,
        final ArrayList<Scope> scopes, final String applicationName) {

    Series<Cookie> cookies = new Series<>(Cookie.class);
    cookies.add(AuthorizationBaseServerResource.ClientCookieID, sessionId);

    // scopesを文字列配列に変換する
    ArrayList<String> strScopes = ScopeUtil.scopesToStrings(scopes);
    
    ArrayList<String> actions = new ArrayList<>();
    actions.add(AuthPageServerResource.ACTION_ACCEPT);
    
    ArrayList<String> applicationNames = new ArrayList<>();
    applicationNames.add(applicationName);
    
    AuthPageServerResource.getQuery().put(AuthPageServerResource.SCOPE, strScopes);
    AuthPageServerResource.getQuery().put(AuthPageServerResource.GRANTED_SCOPE, new ArrayList<String>());
    AuthPageServerResource.getQuery().put(AuthPageServerResource.ACTION, actions);
    AuthPageServerResource.getQuery().put(AuthPageServerResource.APPLICATION_NAME, applicationNames);

    Request request = new Request();
    request.setCookies(cookies);
    Response response = new Response(request);
    AuthPageServerResource.init(request, response);

    RedirectRepresentation redirectRepresentation = null;
    try {
        Representation representation = AuthPageServerResource.showPage();
        if (representation != null) {
            if (representation instanceof RedirectRepresentation) {
                redirectRepresentation = (RedirectRepresentation) representation;
            }
        }
    } catch (OAuthException e) {
        e.printStackTrace();
    }

    return redirectRepresentation;
}
 
Example 3
Source File: RestComponent.java    From microservices-comparison with Apache License 2.0 5 votes vote down vote up
@Inject
public RestComponent(@Hello Application helloApp, @Car Application carApp, Verifier authTokenVerifier) {
    getClients().add(Protocol.HTTPS);
    Server secureServer = getServers().add(Protocol.HTTPS, 8043);
    Series<Parameter> parameters = secureServer.getContext().getParameters();
    parameters.add("sslContextFactory", "org.restlet.engine.ssl.DefaultSslContextFactory");
    parameters.add("keyStorePath", System.getProperty("javax.net.ssl.keyStorePath"));
    getDefaultHost().attach("/api/hello", secure(helloApp, authTokenVerifier, "ame"));
    getDefaultHost().attach("/api/cars", secure(carApp, authTokenVerifier, "ame"));
    replaceConverter(JacksonConverter.class, new JacksonCustomConverter());
}
 
Example 4
Source File: ServerResource.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected void addAllowOrigin()
{
    Series<Header> responseHeaders = (Series<Header>) getResponse().getAttributes().get("org.restlet.http.headers");
    if (responseHeaders == null)
    {
        responseHeaders = new Series<>(Header.class);
        getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
    }
    responseHeaders.add("Access-Control-Allow-Origin", "*");
    responseHeaders.add("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
    responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
    responseHeaders.add("Access-Control-Allow-Credentials", "false");
    responseHeaders.add("Access-Control-Max-Age", "60");
}
 
Example 5
Source File: ServerResource.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected void addAllowOrigin()
{
    Series<Header> responseHeaders = (Series<Header>) getResponse().getAttributes().get("org.restlet.http.headers");
    if (responseHeaders == null)
    {
        responseHeaders = new Series<>(Header.class);
        getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
    }
    responseHeaders.add("Access-Control-Allow-Origin", "*");
    responseHeaders.add("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
    responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
    responseHeaders.add("Access-Control-Allow-Credentials", "false");
    responseHeaders.add("Access-Control-Max-Age", "60");
}
 
Example 6
Source File: RestService.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws UnknownHostException {
	if (isEnabled) {
		final boolean isRunning = this.isRunning.getAndSet(true);
		if (!isRunning) {
			if (isEnabled && port <= 0) {
				this.isEnabled = false;
				TestServerConsole.log("Could not start RestService. Parameter missing: 'server.service.rest.port'", 1, TestServerServiceEnum.TEST_SERVER);
			}
			 
		    Component component = new Component();  

		    Server s = component.getServers().add(isSsl ? Protocol.HTTPS : Protocol.HTTP, InetAddress.getLocalHost().getHostAddress(), port);
		    
		    if (isSsl) {
			    Series<Parameter> parameters = s.getContext().getParameters();				    
			    parameters.add("keystorePath", QOS_KEY_FILE_ABSOLUTE);
			    parameters.add("keystorePassword", TestServer.QOS_KEY_PASSWORD);
			    parameters.add("keyPassword", TestServer.QOS_KEY_PASSWORD);
			    parameters.add("keystoreType", TestServer.QOS_KEY_TYPE);
		    }

		    component.getDefaultHost().attach("", new RestletApplication());
		    
		    try {
				component.start();
				TestServerConsole.log("[" + getName() + "] started: " + toString(), 1, TestServerServiceEnum.TEST_SERVER);
			} catch (Exception e) {
				TestServerConsole.error(getName(), e, 0, TestServerServiceEnum.TEST_SERVER);
			}  
		}
	}
}
 
Example 7
Source File: ServerResource.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected void addAllowOrigin()
{
    Series<Header> responseHeaders = (Series<Header>) getResponse().getAttributes().get("org.restlet.http.headers");
    if (responseHeaders == null)
    {
        responseHeaders = new Series<Header>(Header.class);
        getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
    }
    responseHeaders.add("Access-Control-Allow-Origin", "*");
    responseHeaders.add("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
    responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
    responseHeaders.add("Access-Control-Allow-Credentials", "false");
    responseHeaders.add("Access-Control-Max-Age", "60");
}
 
Example 8
Source File: LocalOAuth2Main.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * AuthorizationServerResourceを実行.
 * 
 * @param client クライアント情報
 * @param initialize 初期化フラグ(trueにするとContextを初期化する)
 * @param sessionId セッションID(引き継ぐセッションIDが存在すれば指定する、無ければnullを設定する)
 * @return not null: RedirectRepresentation型の戻り値を返す / null: エラー
 * @throws AuthorizationException Authorization例外.
 */
private 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, mClientManager, mTokenManager);
    
    // 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 9
Source File: LocalOAuth2Main.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * LoginPageServerResourceを実行する.
 * 
 * @param userId ユーザーID
 * @param password パスワード
 * @return 戻り値(ResultRepresentation)
 */
private 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(this);
    } catch (OAuthException e) {
        resultRepresentation = new ResultRepresentation();
        resultRepresentation.setResult(false);
        resultRepresentation.setError(e.getMessage(), e.getErrorDescription());
    }

    return resultRepresentation;
}