Search in sources :

Example 96 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project pulsar by yahoo.

the class DiscoveryServiceWebTest method testTlsEnable.

@Test
public void testTlsEnable() throws Exception {
    // 1. start server with tls enable
    int port = nextFreePort();
    int tlsPort = nextFreePort();
    ServiceConfig config = new ServiceConfig();
    config.setWebServicePort(port);
    config.setWebServicePortTls(tlsPort);
    config.setTlsEnabled(true);
    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    ServerManager server = new ServerManager(config);
    DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
    Map<String, String> params = new TreeMap<>();
    params.put("zookeeperServers", "dummy-value");
    params.put("zookeeperClientFactoryClass", DiscoveryZooKeeperClientFactoryImpl.class.getName());
    server.addServlet("/", DiscoveryServiceServlet.class, params);
    server.start();
    // 2. get ZookeeperCacheLoader to add more brokers
    final String redirect_broker_host = "broker-1";
    List<String> brokers = Lists.newArrayList(redirect_broker_host);
    brokers.stream().forEach(b -> {
        try {
            final String brokerUrl = b + ":" + port;
            final String brokerUrlTls = b + ":" + tlsPort;
            LoadReport report = new LoadReport("http://" + brokerUrl, "https://" + brokerUrlTls, null, null);
            String reportData = ObjectMapperFactory.getThreadLocal().writeValueAsString(report);
            ZkUtils.createFullPathOptimistic(mockZookKeeper, LOADBALANCE_BROKERS_ROOT + "/" + brokerUrl, reportData.getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        }
    });
    // 3. https request with tls enable at server side
    String serviceUrl = String.format("https://localhost:%s/", tlsPort);
    String requestUrl = serviceUrl + "admin/namespaces/p1/c1/n1";
    KeyManager[] keyManagers = null;
    TrustManager[] trustManagers = InsecureTrustManagerFactory.INSTANCE.getTrustManagers();
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagers, trustManagers, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
    try {
        InputStream response = new URL(requestUrl).openStream();
        fail("it should give unknown host exception as: discovery service redirects request to: " + redirect_broker_host);
    } catch (Exception e) {
        // 4. Verify: server accepts https request and redirected to one of the available broker host defined into
        // zk. and as broker-service is not up: it should give "UnknownHostException with host=broker-url"
        String host = e.getLocalizedMessage();
        assertEquals(e.getClass(), UnknownHostException.class);
        assertTrue(host.startsWith(redirect_broker_host));
    }
    server.stop();
}
Also used : ServerManager(com.yahoo.pulsar.discovery.service.server.ServerManager) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) TreeMap(java.util.TreeMap) URL(java.net.URL) KeeperException(org.apache.zookeeper.KeeperException) RestException(com.yahoo.pulsar.discovery.service.web.RestException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnknownHostException(java.net.UnknownHostException) TrustManager(javax.net.ssl.TrustManager) ServiceConfig(com.yahoo.pulsar.discovery.service.server.ServiceConfig) LoadReport(com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) KeyManager(javax.net.ssl.KeyManager) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 97 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project pulsar by yahoo.

the class DiscoveryServiceWebTest method testRiderectUrlWithServerStarted.

@Test
public void testRiderectUrlWithServerStarted() throws Exception {
    // 1. start server
    int port = nextFreePort();
    ServiceConfig config = new ServiceConfig();
    config.setWebServicePort(port);
    ServerManager server = new ServerManager(config);
    DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
    Map<String, String> params = new TreeMap<>();
    params.put("zookeeperServers", "dummy-value");
    params.put("zookeeperClientFactoryClass", DiscoveryZooKeeperClientFactoryImpl.class.getName());
    server.addServlet("/", DiscoveryServiceServlet.class, params);
    server.start();
    // 2. create znode for each broker
    List<String> brokers = Lists.newArrayList("broker-1", "broker-2", "broker-3");
    brokers.stream().forEach(b -> {
        try {
            final String broker = b + ":15000";
            LoadReport report = new LoadReport("http://" + broker, null, null, null);
            String reportData = ObjectMapperFactory.getThreadLocal().writeValueAsString(report);
            ZkUtils.createFullPathOptimistic(mockZookKeeper, LOADBALANCE_BROKERS_ROOT + "/" + broker, reportData.getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        }
    });
    String serviceUrl = server.getServiceUri().toString();
    String requestUrl = serviceUrl + "admin/namespaces/p1/c1/n1";
    /**
         * 3. verify : every time when vip receives a request: it redirects to above brokers sequentially and client
         * must get unknown host exception with above brokers in a sequential manner.
         **/
    assertEquals(brokers, validateRequest(brokers, HttpMethod.PUT, requestUrl, new BundlesData(1)), "redirection failed");
    assertEquals(brokers, validateRequest(brokers, HttpMethod.GET, requestUrl, null), "redirection failed");
    assertEquals(brokers, validateRequest(brokers, HttpMethod.POST, requestUrl, new BundlesData(1)), "redirection failed");
    server.stop();
}
Also used : ServerManager(com.yahoo.pulsar.discovery.service.server.ServerManager) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) TreeMap(java.util.TreeMap) ServiceConfig(com.yahoo.pulsar.discovery.service.server.ServiceConfig) LoadReport(com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 98 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project indy by Commonjava.

the class ImpliedRepoMetadataManager method updateImpliedBy.

public void updateImpliedBy(final ArtifactStore store, final ArtifactStore origin) throws ImpliedReposException {
    final String metadata = store.getMetadata(IMPLIED_BY_STORES);
    ImpliedRemotesWrapper wrapper;
    if (metadata == null) {
        wrapper = new ImpliedRemotesWrapper(Collections.singletonList(origin.getKey()));
    } else {
        try {
            wrapper = mapper.readValue(metadata, ImpliedRemotesWrapper.class);
            if (!wrapper.addItem(origin.getKey())) {
                // nothing to change; set to null to signal that nothing should change.
                wrapper = null;
            }
        } catch (final IOException e) {
            throw new ImpliedReposException("Failed to de-serialize implied-by stores from: %s\nJSON: %s\nError: %s", e, store.getKey(), metadata, e.getMessage());
        }
    }
    if (wrapper != null) {
        try {
            store.setMetadata(IMPLIED_BY_STORES, mapper.writeValueAsString(wrapper));
        } catch (final JsonProcessingException e) {
            throw new ImpliedReposException("Failed to serialize implied-by stores to: %s\nJSON: %s\nError: %s", e, store.getKey(), metadata, e.getMessage());
        }
    }
}
Also used : IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ImpliedReposException(org.commonjava.indy.implrepo.ImpliedReposException)

Example 99 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project indy by Commonjava.

the class ImpliedRepoMetadataManager method addImpliedMetadata.

// TODO: Need to deal with pre-existing implications.
public void addImpliedMetadata(final ArtifactStore origin, final List<ArtifactStore> implied) throws ImpliedReposException {
    try {
        final List<StoreKey> impliedKeys = new ArrayList<>(implied.size());
        for (final ArtifactStore store : implied) {
            impliedKeys.add(store.getKey());
            updateImpliedBy(store, origin);
        }
        origin.setMetadata(IMPLIED_STORES, mapper.writeValueAsString(new ImpliedRemotesWrapper(impliedKeys)));
    } catch (final JsonProcessingException e) {
        throw new ImpliedReposException("Failed to serialize implied stores: %s to JSON: %s. Error: %s", e, implied, origin.getKey(), e.getMessage());
    }
}
Also used : ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ArrayList(java.util.ArrayList) StoreKey(org.commonjava.indy.model.core.StoreKey) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ImpliedReposException(org.commonjava.indy.implrepo.ImpliedReposException)

Example 100 with JsonProcessingException

use of com.fasterxml.jackson.core.JsonProcessingException in project oxAuth by GluuFederation.

the class ServletLoggingFilter method getResponseDescription.

protected String getResponseDescription(ResponseWrapper responseWrapper) {
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatus(responseWrapper.getStatus());
    httpResponse.setHeaders(responseWrapper.getHeaders());
    try {
        return OBJECT_MAPPER.writeValueAsString(httpResponse);
    } catch (JsonProcessingException e) {
        log.warn("Cannot serialize Response to JSON", e);
        return null;
    }
}
Also used : HttpResponse(org.xdi.oxauth.audit.debug.entity.HttpResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)117 IOException (java.io.IOException)28 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)26 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)11 Test (org.junit.Test)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 InputStream (java.io.InputStream)7 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)6 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)6 OutputStreamWriter (java.io.OutputStreamWriter)6 Map (java.util.Map)6 Status (com.ctrip.platform.dal.daogen.domain.Status)5 TaskDTO (com.linkedin.thirdeye.datalayer.dto.TaskDTO)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 List (java.util.List)4 IncorrectRPCMethodException (com.exalttech.trex.remote.exceptions.IncorrectRPCMethodException)3 InvalidRPCResponseException (com.exalttech.trex.remote.exceptions.InvalidRPCResponseException)3