Package org.restlet.data

Examples of org.restlet.data.ClientInfo


        this(request.getMethod(), new Reference(request.getResourceRef()),
                request.getEntity());
        challengeResponse = request.getChallengeResponse();

        // Copy client info
        ClientInfo rci = request.getClientInfo();
        clientInfo = new ClientInfo();

        for (Preference<CharacterSet> o : rci.getAcceptedCharacterSets()) {
            clientInfo.getAcceptedCharacterSets().add(o);
        }

        for (Preference<Encoding> o : rci.getAcceptedEncodings()) {
            clientInfo.getAcceptedEncodings().add(o);
        }

        for (Preference<Language> o : rci.getAcceptedLanguages()) {
            clientInfo.getAcceptedLanguages().add(o);
        }

        for (Preference<MediaType> o : rci.getAcceptedMediaTypes()) {
            clientInfo.getAcceptedMediaTypes().add(o);
        }

        clientInfo.setAddress(rci.getAddress());
        clientInfo.setAgent(rci.getAgent());

        for (String o : rci.getForwardedAddresses()) {
            clientInfo.getForwardedAddresses().add(o);
        }

        clientInfo.setFrom(rci.getFrom());
        clientInfo.setPort(rci.getPort());

        clientInfo.setAgentAttributes(rci.getAgentAttributes());
        clientInfo.setAgentProducts(rci.getAgentProducts());
        clientInfo.setAuthenticated(rci.isAuthenticated());

        for (org.restlet.data.Expectation o : rci.getExpectations()) {
            clientInfo.getExpectations().add(o);
        }

        for (java.security.Principal o : rci.getPrincipals()) {
            clientInfo.getPrincipals().add(o);
        }

        for (org.restlet.security.Role o : rci.getRoles()) {
            clientInfo.getRoles().add(o);
        }

        clientInfo.setUser(rci.getUser());

        // Copy conditions
        conditions = new Conditions();

        for (Tag o : request.getConditions().getMatch()) {
View Full Code Here


     *
     * @return The client-specific information.
     */
    public ClientInfo getClientInfo() {
        // Lazy initialization with double-check.
        ClientInfo c = this.clientInfo;
        if (c == null) {
            synchronized (this) {
                c = this.clientInfo;
                if (c == null) {
                    this.clientInfo = c = new ClientInfo();
                }
            }
        }
        return c;
    }
View Full Code Here

     */
    public Graph getLinks() {
        Graph result = this.links;

        if (result == null) {
            ClientInfo currentInfo = getClientInfo();

            // Customize the preferences to maximize the chance of getting RDF
            ClientInfo newInfo = new ClientInfo();
            newInfo.getAcceptedMediaTypes().add(
                    new Preference<MediaType>(MediaType.APPLICATION_RDF_XML));
            newInfo.getAcceptedMediaTypes().add(
                    new Preference<MediaType>(MediaType.TEXT_RDF_N3));
            newInfo.getAcceptedMediaTypes().add(
                    new Preference<MediaType>(MediaType.TEXT_RDF_NTRIPLES));
            newInfo.getAcceptedMediaTypes()
                    .add(
                            new Preference<MediaType>(
                                    MediaType.APPLICATION_RDF_TURTLE));
            newInfo.getAcceptedMediaTypes().add(
                    new Preference<MediaType>(MediaType.TEXT_XML, 0.5F));
            newInfo.getAcceptedMediaTypes().add(
                    new Preference<MediaType>(MediaType.TEXT_PLAIN, 0.4F));
            newInfo.getAcceptedMediaTypes().add(
                    new Preference<MediaType>(MediaType.APPLICATION_ALL_XML,
                            0.3F));

            // Attempt to retrieve the RDF representation
            try {
View Full Code Here

    protected <T> T handle(Method method, Object entity, Class<T> resultClass)
            throws ResourceException {
        T result = null;
        org.restlet.service.ConverterService cs = getConverterService();
        List<? extends Variant> variants = cs.getVariants(resultClass, null);
        ClientInfo clientInfo = getClientInfo();

        if (clientInfo.getAcceptedMediaTypes().isEmpty()) {
            cs.updatePreferences(clientInfo.getAcceptedMediaTypes(),
                    resultClass);
        }

        result = toObject(
                handle(method,
                        (entity == null) ? null : toRepresentation(entity,
                                clientInfo.getPreferredVariant(variants,
                                        getMetadataService())), clientInfo),
                resultClass);
        return result;
    }
View Full Code Here

     *            The preferred result media type.
     * @return The optional response entity.
     */
    protected Representation handle(Method method, Representation entity,
            MediaType mediaType) {
        return handle(method, entity, new ClientInfo(mediaType));
    }
View Full Code Here

     *
     * @return The client-specific information.
     */
    @Override
    public ClientInfo getClientInfo() {
        ClientInfo result = super.getClientInfo();

        if (!this.clientAdded) {
            if (getHeaders() != null) {
                // Extract the header values
                String acceptMediaType = getHeaders().getValues(
                        HeaderConstants.HEADER_ACCEPT);
                String acceptCharset = getHeaders().getValues(
                        HeaderConstants.HEADER_ACCEPT_CHARSET);
                String acceptEncoding = getHeaders().getValues(
                        HeaderConstants.HEADER_ACCEPT_ENCODING);
                String acceptLanguage = getHeaders().getValues(
                        HeaderConstants.HEADER_ACCEPT_LANGUAGE);
                String expect = getHeaders().getValues(
                        HeaderConstants.HEADER_EXPECT);

                // Parse the headers and update the call preferences

                // Parse the Accept* headers. If an error occurs during the
                // parsing of each header, the error is traced and we keep on
                // with the other headers.
                try {
                    PreferenceReader.addCharacterSets(acceptCharset, result);
                } catch (Exception e) {
                    this.context.getLogger().log(Level.INFO, e.getMessage());
                }

                try {
                    PreferenceReader.addEncodings(acceptEncoding, result);
                } catch (Exception e) {
                    this.context.getLogger().log(Level.INFO, e.getMessage());
                }

                try {
                    PreferenceReader.addLanguages(acceptLanguage, result);
                } catch (Exception e) {
                    this.context.getLogger().log(Level.INFO, e.getMessage());
                }

                try {
                    PreferenceReader.addMediaTypes(acceptMediaType, result);
                } catch (Exception e) {
                    this.context.getLogger().log(Level.INFO, e.getMessage());
                }

                try {
                    ExpectationReader.addValues(expect, result);
                } catch (Exception e) {
                    this.context.getLogger().log(Level.INFO, e.getMessage());
                }

                // Set other properties
                result.setAgent(getHeaders().getValues(
                        HeaderConstants.HEADER_USER_AGENT));
                result.setFrom(getHeaders().getFirstValue(
                        HeaderConstants.HEADER_FROM));
                result.setAddress(getConnection().getAddress());
                result.setPort(getConnection().getPort());

                if (userPrincipal != null) {
                    result.getPrincipals().add(userPrincipal);
                }

                if (this.context != null) {
                    // Special handling for the non standard but common
                    // "X-Forwarded-For" header.
                    final boolean useForwardedForHeader = Boolean
                            .parseBoolean(this.context.getParameters()
                                    .getFirstValue("useForwardedForHeader",
                                            false));
                    if (useForwardedForHeader) {
                        // Lookup the "X-Forwarded-For" header supported by
                        // popular
                        // proxies and caches.
                        final String header = getHeaders().getValues(
                                HeaderConstants.HEADER_X_FORWARDED_FOR);
                        if (header != null) {
                            final String[] addresses = header.split(",");
                            for (int i = 0; i < addresses.length; i++) {
                                String address = addresses[i].trim();
                                result.getForwardedAddresses().add(address);
                            }
                        }
                    }
                }
            }
View Full Code Here

        }
    }

    public void setAcceptedMediaTypes(Request request) {
        checkNotNull(request);
        ClientInfo clientInfo = request.getClientInfo();
        List<Preference<MediaType>> preferenceList = new ArrayList<Preference<MediaType>>();
        for (MediaType mediaType : typeToParser.keySet()) {
            preferenceList.add(new Preference<MediaType>(mediaType));
        }
        clientInfo.setAcceptedMediaTypes(preferenceList);
    }
View Full Code Here

                requestHeaders.add(HttpConstants.HEADER_REFERRER, request
                        .getReferrerRef().toString());
            }

            // Add the preferences
            final ClientInfo client = request.getClientInfo();
            if (client.getAcceptedMediaTypes().size() > 0) {
                try {
                    requestHeaders.add(HttpConstants.HEADER_ACCEPT,
                            PreferenceUtils.format(client
                                    .getAcceptedMediaTypes()));
                } catch (IOException ioe) {
                    getLogger().log(Level.WARNING,
                            "Unable to format the HTTP Accept header", ioe);
                }
            } else {
                requestHeaders.add(HttpConstants.HEADER_ACCEPT, MediaType.ALL
                        .getName());
            }

            if (client.getAcceptedCharacterSets().size() > 0) {
                try {
                    requestHeaders.add(HttpConstants.HEADER_ACCEPT_CHARSET,
                            PreferenceUtils.format(client
                                    .getAcceptedCharacterSets()));
                } catch (IOException ioe) {
                    getLogger().log(Level.WARNING,
                            "Unable to format the HTTP Accept header", ioe);
                }
            }

            if (client.getAcceptedEncodings().size() > 0) {
                try {
                    requestHeaders.add(HttpConstants.HEADER_ACCEPT_ENCODING,
                            PreferenceUtils.format(client
                                    .getAcceptedEncodings()));
                } catch (IOException ioe) {
                    getLogger().log(Level.WARNING,
                            "Unable to format the HTTP Accept header", ioe);
                }
            }

            if (client.getAcceptedLanguages().size() > 0) {
                try {
                    requestHeaders.add(HttpConstants.HEADER_ACCEPT_LANGUAGE,
                            PreferenceUtils.format(client
                                    .getAcceptedLanguages()));
                } catch (IOException ioe) {
                    getLogger().log(Level.WARNING,
                            "Unable to format the HTTP Accept header", ioe);
                }
View Full Code Here

  @Ignore("Fails when not running on VPN; ignoring for now")
  public void testResolveIP() {
    Request restlet = Mockito.mock(Request.class);
    Map<String, Object> attributes = Mockito.mock(Map.class);
    Form form = Mockito.mock(Form.class);
    ClientInfo clientInfo = new ClientInfo();

    Mockito.doReturn(attributes).when(restlet).getAttributes();
    Mockito.doReturn(form).when(attributes).get("org.restlet.http.headers");
    Mockito.doReturn(clientInfo).when(restlet).getClientInfo();

    Mockito.doReturn("").when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals(null, RemoteIPFinder.findIP(restlet));

    Mockito.doReturn(null).when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals(null, RemoteIPFinder.findIP(restlet));

    Mockito.doReturn("127.0.0.1").when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals("127.0.0.1", RemoteIPFinder.findIP(restlet));

    Mockito.doReturn("missing, 127.0.0.2, unknown, 127.0.0.1").when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals("127.0.0.2", RemoteIPFinder.findIP(restlet));

    Mockito.doReturn("127.0.0.3, 127.0.0.2, 127.0.0.1").when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals("127.0.0.3", RemoteIPFinder.findIP(restlet));

    Mockito.doReturn("localhost").when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals("localhost", RemoteIPFinder.findIP(restlet));

    clientInfo.setAddresses(null);
    Mockito.doReturn("client, proxy1, proxy2").when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals(null, RemoteIPFinder.findIP(restlet));

    clientInfo.setAddresses(Collections.<String>emptyList());
    Mockito.doReturn("client, proxy1, proxy2").when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals(null, RemoteIPFinder.findIP(restlet));

    // restlet1x clientInfo addresses are the last known upstream address + reverse of XFF
    clientInfo.setAddresses(Arrays.asList("upstream", "proxy2", "proxy1", "client"));
    Mockito.doReturn("client, proxy1, proxy2").when(form).getFirstValue(org.sonatype.nexus.web.RemoteIPFinder.FORWARD_HEADER);
    Assert.assertEquals("upstream", RemoteIPFinder.findIP(restlet));
  }
View Full Code Here

    final Conditions conditions = new Conditions();
    when(request.getConditions()).thenReturn(conditions);

    // we need more mock responses as getResourceStoreRequest tries to gather
    // as much info as it can (from client, reference, baseUrl etc
    when(request.getClientInfo()).thenReturn(new ClientInfo());
    when(request.getOriginalRef()).thenReturn(reference);
    final ResourceStoreRequest rsr = underTest.getResourceStoreRequest(request, "/some/path");

    assertThat(rsr.getIfNoneMatch(), nullValue());
  }
View Full Code Here

TOP

Related Classes of org.restlet.data.ClientInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.