1 : /******************************************************************************
2 : * $Id: ogrelasticdatasource.cpp 23848 2012-01-31 20:49:42Z rouault $
3 : *
4 : * Project: ElasticSearch Translator
5 : * Purpose:
6 : * Author:
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2011, Adam Estrada
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #pragma warning( disable : 4251 )
31 :
32 : #include "ogr_elastic.h"
33 : #include "cpl_conv.h"
34 : #include "cpl_string.h"
35 : #include "cpl_csv.h"
36 : #include "cpl_http.h"
37 :
38 : CPL_CVSID("$Id: ogrelasticdatasource.cpp 23848 2012-01-31 20:49:42Z rouault $");
39 :
40 : /************************************************************************/
41 : /* OGRElasticDataSource() */
42 : /************************************************************************/
43 :
44 4 : OGRElasticDataSource::OGRElasticDataSource() {
45 4 : papoLayers = NULL;
46 4 : nLayers = 0;
47 4 : pszName = NULL;
48 4 : pszMapping = NULL;
49 4 : pszWriteMap = NULL;
50 4 : }
51 :
52 : /************************************************************************/
53 : /* ~OGRElasticDataSource() */
54 : /************************************************************************/
55 :
56 4 : OGRElasticDataSource::~OGRElasticDataSource() {
57 8 : for (int i = 0; i < nLayers; i++)
58 4 : delete papoLayers[i];
59 4 : CPLFree(papoLayers);
60 4 : CPLFree(pszName);
61 4 : CPLFree(pszMapping);
62 4 : CPLFree(pszWriteMap);
63 4 : }
64 :
65 : /************************************************************************/
66 : /* TestCapability() */
67 : /************************************************************************/
68 :
69 0 : int OGRElasticDataSource::TestCapability(const char * pszCap) {
70 0 : if (EQUAL(pszCap, ODsCCreateLayer))
71 0 : return TRUE;
72 : else
73 0 : return FALSE;
74 : }
75 :
76 : /************************************************************************/
77 : /* GetLayer() */
78 : /************************************************************************/
79 :
80 0 : OGRLayer *OGRElasticDataSource::GetLayer(int iLayer) {
81 0 : if (iLayer < 0 || iLayer >= nLayers)
82 0 : return NULL;
83 : else
84 0 : return papoLayers[iLayer];
85 : }
86 :
87 : /************************************************************************/
88 : /* CreateLayer() */
89 : /************************************************************************/
90 :
91 4 : OGRLayer * OGRElasticDataSource::CreateLayer(const char * pszLayerName,
92 : OGRSpatialReference *poSRS,
93 : OGRwkbGeometryType eType,
94 : char ** papszOptions) {
95 4 : nLayers++;
96 4 : papoLayers = (OGRElasticLayer **) CPLRealloc(papoLayers, nLayers * sizeof (OGRElasticLayer*));
97 4 : papoLayers[nLayers - 1] = new OGRElasticLayer(pszName, pszLayerName, this, poSRS, TRUE);
98 :
99 4 : return papoLayers[nLayers - 1];
100 : }
101 :
102 : /************************************************************************/
103 : /* Open() */
104 : /************************************************************************/
105 :
106 0 : int OGRElasticDataSource::Open(const char * pszFilename, int bUpdateIn) {
107 : CPLError(CE_Failure, CPLE_NotSupported,
108 0 : "OGR/Elastic driver does not support opening a file");
109 0 : return FALSE;
110 : }
111 :
112 :
113 : /************************************************************************/
114 : /* DeleteIndex() */
115 : /************************************************************************/
116 :
117 4 : void OGRElasticDataSource::DeleteIndex(const CPLString &url) {
118 4 : char** papszOptions = NULL;
119 4 : papszOptions = CSLAddNameValue(papszOptions, "CUSTOMREQUEST", "DELETE");
120 4 : CPLHTTPResult* psResult = CPLHTTPFetch(url, papszOptions);
121 4 : CSLDestroy(papszOptions);
122 4 : if (psResult) {
123 4 : CPLHTTPDestroyResult(psResult);
124 : }
125 4 : }
126 :
127 : /************************************************************************/
128 : /* UploadFile() */
129 : /************************************************************************/
130 :
131 8 : void OGRElasticDataSource::UploadFile(const CPLString &url, const CPLString &data) {
132 8 : char** papszOptions = NULL;
133 8 : papszOptions = CSLAddNameValue(papszOptions, "POSTFIELDS", data.c_str());
134 : papszOptions = CSLAddNameValue(papszOptions, "HEADERS",
135 8 : "Content-Type: application/x-javascript; charset=UTF-8");
136 :
137 8 : CPLHTTPResult* psResult = CPLHTTPFetch(url, papszOptions);
138 8 : CSLDestroy(papszOptions);
139 8 : if (psResult) {
140 8 : CPLHTTPDestroyResult(psResult);
141 : }
142 8 : }
143 :
144 : /************************************************************************/
145 : /* Create() */
146 : /************************************************************************/
147 :
148 4 : int OGRElasticDataSource::Create(const char *pszFilename,
149 : char **papszOptions) {
150 :
151 4 : this->pszName = CPLStrdup(pszFilename);
152 :
153 4 : const char* pszMetaFile = CPLGetConfigOption("ES_META", NULL);
154 4 : const char* pszWriteMap = CPLGetConfigOption("ES_WRITEMAP", NULL);;
155 4 : this->bOverwrite = CSLTestBoolean(CPLGetConfigOption("ES_OVERWRITE", "0"));
156 4 : this->nBulkUpload = (int) CPLAtof(CPLGetConfigOption("ES_BULK", "0"));
157 :
158 4 : if (pszWriteMap != NULL) {
159 0 : this->pszWriteMap = CPLStrdup(pszWriteMap);
160 : }
161 :
162 : // Read in the meta file from disk
163 4 : if (pszMetaFile != NULL)
164 : {
165 : int fsize;
166 : char *fdata;
167 : FILE *fp;
168 :
169 0 : fp = fopen(pszMetaFile, "rb");
170 0 : if (fp != NULL) {
171 0 : fseek(fp, 0, SEEK_END);
172 0 : fsize = (int) ftell(fp);
173 :
174 0 : fdata = (char *) malloc(fsize + 1);
175 :
176 0 : fseek(fp, 0, SEEK_SET);
177 0 : fread(fdata, fsize, 1, fp);
178 0 : fdata[fsize] = 0;
179 0 : this->pszMapping = fdata;
180 0 : fclose(fp);
181 : }
182 : }
183 :
184 : // Do a status check to ensure that the server is valid
185 4 : CPLHTTPResult* psResult = CPLHTTPFetch(CPLSPrintf("%s/_status", pszFilename), NULL);
186 4 : int bOK = (psResult != NULL && psResult->pszErrBuf == NULL);
187 4 : if (!bOK)
188 : {
189 : CPLError(CE_Failure, CPLE_NoWriteAccess,
190 2 : "Could not connect to server");
191 : }
192 :
193 4 : CPLHTTPDestroyResult(psResult);
194 :
195 4 : return bOK;
196 : }
|