wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.HTTPClient.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id$
18  */
19 require_once(BASE."wcmf/lib/util/class.Log.php");
20 require_once(BASE."wcmf/lib/presentation/class.Request.php");
21 require_once(BASE."wcmf/lib/presentation/class.Response.php");
22 require_once BASE.'wcmf/3rdparty/zend/Zend/Http/Client.php';
23 
24 set_include_path(get_include_path().PATH_SEPARATOR.BASE.'wcmf/3rdparty/zend');
25 
26 /**
27  * @class HTTPClient
28  * @ingroup Remoting
29  * @brief HTTPClient is used to do calls to other wCMF instances over HTTP.
30  *
31  * @author ingo herwig <ingo@wemove.com>
32  */
34 {
35  private $_client = null;
36  private $_user = null;
37 
38  /**
39  * Constructor
40  * @param serverUrl The url of the other server instance.
41  * @param user The remote user instance.
42  */
43  function __construct($serverUrl, $user)
44  {
45  $this->_client = new Zend_Http_Client($serverUrl, array(
46  'keepalive' => true,
47  'timeout' => 3600
48  )
49  );
50  $this->_client->setMethod(Zend_Http_Client::POST);
51  $this->_client->setCookieJar();
52  $this->_user = $user;
53  }
54  /**
55  * Do a call to the remote server.
56  * @param request A Request instance
57  * @return A Response instance
58  */
59  function call($request)
60  {
61  $response = $this->doRemoteCall($request, false);
62  return $response;
63  }
64  /**
65  * Do a remote call.
66  * @param request The Request instance
67  * @param isLogin True/False wether this request is a login request or not
68  * @return The Response instance
69  */
70  protected function doRemoteCall($request, $isLogin)
71  {
72  // initially login, if no cookie is set
73  $cookyJar = $this->_client->getCookieJar();
74  if (!$isLogin && sizeof($cookyJar->getAllCookies()) == 0) {
75  $response = $this->doLogin();
76  }
77 
78  // do the request
79  $request->setResponseFormat(MSG_FORMAT_JSON);
80  $this->_client->resetParameters();
81  $this->_client->setParameterPost('controller', $request->getSender());
82  $this->_client->setParameterPost('context', $request->getContext());
83  $this->_client->setParameterPost('usr_action', $request->getAction());
84  $this->_client->setParameterPost('format', $request->getFormat());
85  $this->_client->setParameterPost('response_format', $request->getResponseFormat());
86  $this->_client->setParameterPost($request->getData());
87  try {
88  $httpResponse = $this->_client->request();
89  }
90  catch (Exception $ex) {
91  Log::error("Error in remote call to ".$url.":\n".$ex, __FILE__, __LINE__);
92  WCMFException::throwEx("Error in remote call to ".$url.": ".$ex->getMessage(), __FILE__, __LINE__);
93  }
94 
95  // deserialize the response
96  $responseData = JSONUtil::decode($httpResponse->getBody(), true);
97  $response = new ControllerMessage('', '', '', $responseData);
98  $response->setFormat(MSG_FORMAT_JSON);
99  Formatter::deserialize($response);
100 
101  // handle errors
102  if (!$response->getValue('success'))
103  {
104  $errorMsg = $response->getValue('errorMsg');
105  // if the session expired, try to relogin
106  if (strpos('Authorization failed', $errorMsg) === 0 && !$isLogin) {
107  $this->doLogin();
108  }
109  $url = $this->_client->getUri();
110  Log::error("Error in remote call to ".$url.": ".$errorMsg."\n".$response->toString(), __FILE__, __LINE__);
111  throw new Exception("Error in remote call: $errorMsg");
112  //WCMFException::throwEx("Error in remote call: ".$errorMsg, __FILE__, __LINE__);
113  }
114  return $response;
115  }
116  /**
117  * Do the login request. If the request is successful,
118  * the session id will be set.
119  * @return True on success
120  */
121  protected function doLogin()
122  {
123  if ($this->_user)
124  {
125  $request = new Request(
126  'LoginController',
127  '',
128  'dologin',
129  array(
130  'login' => $this->_user->getLogin(),
131  'password' => $this->_user->getPassword(),
132  'password_is_encrypted' => true
133  )
134  );
135  $response = $this->doRemoteCall($request, true);
136  if ($response->getValue('success')) {
137  $this->_sessionId = $response->getValue('sid');
138  return true;
139  }
140  }
141  else {
142  WCMFException::throwEx("Remote user required for remote call.", __FILE__, __LINE__);
143  }
144  }
145  /**
146  * Error handling method
147  * @param response The Response instance
148  */
149  protected function handleError($response)
150  {
151  $errorMsg = $response->getValue('errorMsg');
152  Log::error("Error in remote call to ".$this->_serverBase.": ".$errorMsg."\n".$response->toString(), __FILE__, __LINE__);
153  WCMFException::throwEx("Error in remote call to ".$this->_serverBase.": ".$errorMsg, __FILE__, __LINE__);
154  }
155 }
156 ?>
const MSG_FORMAT_JSON
error($message, $category)
Definition: class.Log.php:69
call($request)
handleError($response)
Request holds the request values that are used as input to Controller instances. It is typically inst...
throwEx($message, $file='', $line='')
ControllerMessages are sent between Controllers and are used to transfer data between them...
static decode($value, $assoc=false)
HTTPClient is used to do calls to other wCMF instances over HTTP.
__construct($serverUrl, $user)
deserialize(&$request)
doRemoteCall($request, $isLogin)