source: sandbox/expresso-solr/expressoMail1_2/solrclient/library/Solarium/Client/Adapter/PeclHttp.php @ 7588

Revision 7588, 5.2 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
1<?php
2/**
3 * Copyright 2011 Gasol Wu. PIXNET Digital Media Corporation.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 *    this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 *    this listof conditions and the following disclaimer in the documentation
14 *    and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * The views and conclusions contained in the software and documentation are
29 * those of the authors and should not be interpreted as representing official
30 * policies, either expressed or implied, of the copyright holder.
31 *
32 * @copyright Copyright 2011 Gasol Wu <gasol.wu@gmail.com>
33 * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
34 * @link http://www.solarium-project.org/
35 *
36 * @package Solarium
37 * @subpackage Client
38 */
39
40/**
41 * Pecl HTTP adapter
42 *
43 * @author Gasol Wu <gasol.wu@gmail.com>
44 * @package Solarium
45 * @subpackage Client
46 */
47class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
48{
49
50    /**
51     * Initialization hook
52     *
53     * Checks the availability of pecl_http
54     */
55    protected function _init()
56    {
57        // @codeCoverageIgnoreStart
58        if (!class_exists('HttpRequest', false)) {
59           throw new Solarium_Exception('Pecl_http is not available, install it to use the PeclHttp adapter');
60        }
61
62        parent::_init();
63        // @codeCoverageIgnoreEnd
64    }
65
66    /**
67     * Execute a Solr request using the Pecl Http
68     *
69     * @param Solarium_Client_Request $request
70     * @return Solarium_Client_Response
71     */
72    public function execute($request)
73    {
74        $httpRequest = $this->toHttpRequest($request);
75
76        try {
77            $httpMessage = $httpRequest->send();
78        } catch (Exception $e) {
79            throw new Solarium_Client_HttpException($e->getMessage());
80        }
81
82        return new Solarium_Client_Response(
83            $httpMessage->getBody(),
84            $this->_toRawHeaders($httpMessage)
85        );
86    }
87
88    /**
89     * Convert key/value pair header to raw header.
90     *
91     * <code>
92     * //before
93     * $headers['Content-Type'] = 'text/plain';
94     *
95     * ...
96     *
97     * //after
98     * $headers[0] = 'Content-Type: text/plain';
99     * </code>
100     *
101     * @param $message HttpMessage
102     * @return array
103     */
104    protected function _toRawHeaders($message)
105    {
106        $headers[] = 'HTTP/' . $message->getHttpVersion()
107                   . ' ' . $message->getResponseCode()
108                   . ' ' . $message->getResponseStatus();
109
110        foreach ($message->getHeaders() as $header => $value) {
111            $headers[] = "$header: $value";
112        }
113
114        return $headers;
115    }
116
117    /**
118     *
119     * adapt Solarium_Client_Request to HttpRequest
120     *
121     * {@link http://us.php.net/manual/en/http.constants.php
122     *  HTTP Predefined Constant}
123     *
124     * @param Solarium_Client_Request $request
125     * @param HttpRequest
126     */
127    public function toHttpRequest($request)
128    {
129        $url = $this->getBaseUri() . $request->getUri();
130        $httpRequest = new HttpRequest($url);
131
132        $headers = array();
133        foreach ($request->getHeaders() as $headerLine) {
134            list($header, $value) = explode(':', $headerLine);
135            if ($header = trim($header)) {
136                $headers[$header] = trim($value);
137            }
138        }
139
140        switch($request->getMethod()) {
141        case Solarium_Client_Request::METHOD_GET:
142            $method = HTTP_METH_GET;
143            break;
144        case Solarium_Client_Request::METHOD_POST:
145            $method = HTTP_METH_POST;
146            $httpRequest->setBody($request->getRawData());
147            if (!isset($headers['Content-Type'])) {
148                $headers['Content-Type'] = 'text/xml; charset=utf-8';
149            }
150            break;
151        case Solarium_Client_Request::METHOD_HEAD:
152            $method = HTTP_METH_HEAD;
153            break;
154        default:
155            throw new Solarium_Exception(
156                'Unsupported method: ' . $request->getMethod()
157            );
158        }
159
160        $httpRequest->setMethod($method);
161        $httpRequest->setOptions(array('timeout' => $this->getTimeout()));
162        $httpRequest->setHeaders($headers);
163
164        return $httpRequest;
165    }
166}
Note: See TracBrowser for help on using the repository browser.