Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2013, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.test.integration.management.interfaces;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILED;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILURE_DESCRIPTION;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
 
 import java.net.URI;
 

Author(s):
Ladislav Thon <lthon@redhat.com>
 
 public class HttpManagementInterface implements ManagementInterface {
     public static final String MANAGEMENT_REALM = "ManagementRealm";
 
     private final URI uri;
     private final HttpClient httpClient;
 
     public HttpManagementInterface(String uriSchemeString hostint portString usernameString password) {
         try {
             this. = new URI(uriScheme + "://" + host + ":" + port + "/management");
         } catch (URISyntaxException e) {
             throw new RuntimeException(e);
         }
 
         this. = createHttpClient(hostportusernamepassword);
     }
 
     @Override
     public ModelNode execute(ModelNode operation) {
         String operationJson = operation.toJSONString(true);
         try {
             HttpPost post = new HttpPost();
             post.setEntity(new StringEntity(operationJson.));
             HttpResponse response = .execute(post);
             return parseResponse(response);
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
 
     private ModelNode parseResponse(HttpResponse response) {
         try {
             String content = EntityUtils.toString(response.getEntity());
             int status = response.getStatusLine().getStatusCode();
             ModelNode modelResponse;
             if (status == .) {
                 modelResponse = ModelNode.fromJSONString(content);
             } else {
                 modelResponse = new ModelNode();
                 modelResponse.get().set();
                modelResponse.get().set(content);
            }
            return modelResponse;
        } catch (IOException e) {
            throw new RuntimeException("Unable to read response content as String");
        }
    }
    @Override
    public void close() {
    }
    private static HttpClient createHttpClient(String hostint portString usernameString password) {
        Registry<ConnectionSocketFactoryregistry = RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", PlainConnectionSocketFactory.getSocketFactory())
                        .register("https"new SSLConnectionSocketFactory(SSLContexts.createDefault(), new AllowAllHostnameVerifier()))
                        .build();
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(hostport.),
                        new UsernamePasswordCredentials(usernamepassword));
        HttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
                .setRetryHandler(new StandardHttpRequestRetryHandler(5,true))
                .setDefaultCredentialsProvider(credentialsProvider)
                .build();
        return httpClient;
    }
    public static ManagementInterface create(String hostint portString usernameString password) {
        return new HttpManagementInterface("http"hostportusernamepassword);
    }
    public static ManagementInterface createSecure(String hostint portString usernameString password) {
        return new HttpManagementInterface("https"hostportusernamepassword);
    }
New to GrepCode? Check out our FAQ X