Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 eXo Platform SAS.
   *
   * 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.exoplatform.services.jcr.ext.initializer.impl;
 
 
 import java.net.URL;

Created by The eXo Platform SAS.
Date: 20.03.2009

Author(s):
Alex Reshetnyak
Version:
$Id: RemoteHTTPClient.java 111 2008-11-11 11:11:11Z rainf0x $
 
 public class RemoteHttpClient
 {

   
HTTP_OK. The http response 200.
 
    private static final int HTTP_OK = 200;

   
Url to remote data source.
 
    private final String dataSourceUrl;

   
RemoteHttpClient constructor.

Parameters:
dataSourceUrl the data source url
 
    public RemoteHttpClient(String dataSourceUrl)
    {
       this. = dataSourceUrl;
    }

   
execute.

Parameters:
repositoryName the repository name
workspaceName the workspace name
id the channel id
Returns:
String the response
Throws:
org.exoplatform.services.jcr.ext.initializer.RemoteWorkspaceInitializationException will be generated the RemoteWorkspaceInitializerException
 
    public String execute(String repositoryNameString workspaceNameString id)
    {
       String result = "FAIL";
 
       try
       {
          // execute the GET
 
          String complURL =
              + .. + "/" + repositoryName + "/"
                + workspaceName + "/" + id + "/"
 
          URL url = new URL(complURL);
 
          String userInfo = url.getUserInfo();
          if (userInfo == null || userInfo.split(":").length != 2)
             throw new RemoteWorkspaceInitializationException(
                "Fail remote initializetion : the user name or password not not specified : " + );
         String userName = userInfo.split(":")[0];
         String password = userInfo.split(":")[1];
         HTTPConnection connection = new HTTPConnection(url);
         connection.removeModule(CookieModule.class);
         String realmName = getRealm(complURL);
         connection.addBasicAuthorization(realmNameuserNamepassword);
         HTTPResponse resp = connection.Get(url.getFile());
         result = resp.getText();
         AuthorizationInfo.removeAuthorization(url.getHost(), url.getPort(), "Basic"realmName);
         if (resp.getStatusCode() != )
            throw new RemoteWorkspaceInitializationException("Fail remote initializetion : " + result);
      }
      catch (ModuleException e)
      {
         throw new RemoteWorkspaceInitializationException(e.getMessage(), e);
      }
      catch (ParseException e)
      {
         throw new RemoteWorkspaceInitializationException(e.getMessage(), e);
      }
      catch (IOException e)
      {
         throw new RemoteWorkspaceInitializationException(e.getMessage(), e);
      }
      return result;
   }

   
Get realm by URL.

Parameters:
sUrl URL string.
Returns:
realm name string.
Throws:
java.io.IOException transport exception.
org.exoplatform.common.http.client.ModuleException ModuleException.
   private String getRealm(String sUrlthrows IOExceptionModuleException
   {
      AuthorizationHandler ah = AuthorizationInfo.getAuthHandler();
      try
      {
         URL url = new URL(sUrl);
         HTTPConnection connection = new HTTPConnection(url);
         connection.removeModule(CookieModule.class);
         AuthorizationInfo.setAuthHandler(null);
         HTTPResponse resp = connection.Get(url.getFile());
         String authHeader = resp.getHeader("WWW-Authenticate");
         String realm = authHeader.split("=")[1];
         realm = realm.substring(1, realm.length() - 1);
         return realm;
      }
      finally
      {
         AuthorizationInfo.setAuthHandler(ah);
      }
   }
New to GrepCode? Check out our FAQ X