Esempio n. 1
0
void QgsHttpRequestHandler::setGetMapResponse( const QString& service, QImage* img, int imageQuality = -1 )
{
  Q_UNUSED( service );
  QgsMessageLog::logMessage( "setting getmap response..." );
  if ( img )
  {
    bool png16Bit = ( mFormatString.compare( "image/png; mode=16bit", Qt::CaseInsensitive ) == 0 );
    bool png8Bit = ( mFormatString.compare( "image/png; mode=8bit", Qt::CaseInsensitive ) == 0 );
    bool png1Bit = ( mFormatString.compare( "image/png; mode=1bit", Qt::CaseInsensitive ) == 0 );
    bool isBase64 = mFormatString.endsWith( ";base64", Qt::CaseInsensitive );
    if ( mFormat != "PNG" && mFormat != "JPG" && !png16Bit && !png8Bit && !png1Bit )
    {
      QgsMessageLog::logMessage( "service exception - incorrect image format requested..." );
      setServiceException( QgsMapServiceException( "InvalidFormat", "Output format '" + mFormatString + "' is not supported in the GetMap request" ) );
      return;
    }

    //store the image in a QByteArray and set it directly
    QByteArray ba;
    QBuffer buffer( &ba );
    buffer.open( QIODevice::WriteOnly );

    // Do not use imageQuality for PNG images
    // For now, QImage expects quality to be a range 0-9 for PNG
    if ( mFormat == "PNG" )
    {
      imageQuality = -1;
    }

    if ( png8Bit )
    {
      QVector<QRgb> colorTable;
      medianCut( colorTable, 256, *img );
      QImage palettedImg = img->convertToFormat( QImage::Format_Indexed8, colorTable, Qt::ColorOnly | Qt::ThresholdDither |
                           Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
      palettedImg.save( &buffer, "PNG", imageQuality );
    }
    else if ( png16Bit )
    {
      QImage palettedImg = img->convertToFormat( QImage::Format_ARGB4444_Premultiplied );
      palettedImg.save( &buffer, "PNG", imageQuality );
    }
    else if ( png1Bit )
    {
      QImage palettedImg = img->convertToFormat( QImage::Format_Mono, Qt::MonoOnly | Qt::ThresholdDither |
                           Qt::ThresholdAlphaDither | Qt::NoOpaqueDetection );
      palettedImg.save( &buffer, "PNG", imageQuality );
    }
    else
    {
      img->save( &buffer, mFormat.toUtf8().data(), imageQuality );
    }

    if ( isBase64 )
    {
      ba = ba.toBase64();
    }
    setHttpResponse( &ba, formatToMimeType( mFormat ) );
  }
}
Esempio n. 2
0
void QgsRequestHandler::setGetCoverageResponse( QByteArray* ba )
{
  if ( ba )
  {
    setHttpResponse( *ba, QStringLiteral( "image/tiff" ) );
  }
}
Esempio n. 3
0
void QgsHttpRequestHandler::setGetPrintResponse( QByteArray* ba )
{
  if ( mFormatString.endsWith( ";base64", Qt::CaseInsensitive ) )
  {
    *ba = ba->toBase64();
  }
  setHttpResponse( ba, formatToMimeType( mFormat ) );
}
Esempio n. 4
0
void QgsSOAPRequestHandler::setXmlResponse( const QDomDocument& infoDoc )
{
  QDomDocument featureInfoResponseDoc;

  //Envelope
  QDomElement soapEnvelopeElement = featureInfoResponseDoc.createElement( "soap:Envelope" );
  soapEnvelopeElement.setAttribute( "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/" );
  soapEnvelopeElement.setAttribute( "soap:encoding", "http://schemas.xmlsoap.org/soap/encoding/" );
  featureInfoResponseDoc.appendChild( soapEnvelopeElement );

  //Body
  QDomElement soapBodyElement = featureInfoResponseDoc.createElementNS( "http://schemas.xmlsoap.org/soap/envelope/", "soap:Body" );
  soapEnvelopeElement.appendChild( soapBodyElement );

  soapBodyElement.appendChild( infoDoc.documentElement() );

  //now set message
  QByteArray ba = featureInfoResponseDoc.toByteArray();
  setHttpResponse( &ba, "text/xml" );
}
Esempio n. 5
0
void QgsHttpRequestHandler::setServiceException( QgsMapServiceException ex )
{
  mException = &ex;
  //create Exception DOM document
  QDomDocument exceptionDoc;
  QDomElement serviceExceptionReportElem = exceptionDoc.createElement( "ServiceExceptionReport" );
  serviceExceptionReportElem.setAttribute( "version", "1.3.0" );
  serviceExceptionReportElem.setAttribute( "xmlns", "http://www.opengis.net/ogc" );
  exceptionDoc.appendChild( serviceExceptionReportElem );
  QDomElement serviceExceptionElem = exceptionDoc.createElement( "ServiceException" );
  serviceExceptionElem.setAttribute( "code", ex.code() );
  QDomText messageText = exceptionDoc.createTextNode( ex.message() );
  serviceExceptionElem.appendChild( messageText );
  serviceExceptionReportElem.appendChild( serviceExceptionElem );

  QByteArray ba = exceptionDoc.toByteArray();
  // Clear response headers and body and set new exception
  // TODO: check for headersSent()
  clearHeaders();
  clearBody();
  setHttpResponse( &ba, "text/xml" );
}
Esempio n. 6
0
void QgsSOAPRequestHandler::setServiceException( const QgsMapServiceException& ex )
{
  //create response document
  QDomDocument soapResponseDoc;
  //Envelope element
  QDomElement soapEnvelopeElement = soapResponseDoc.createElement( "soap:Envelope" );
  soapEnvelopeElement.setAttribute( "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/" );
  soapEnvelopeElement.setAttribute( "soap:encoding", "http://schemas.xmlsoap.org/soap/encoding/" );
  soapResponseDoc.appendChild( soapEnvelopeElement );

  //Body element
  QDomElement soapBodyElement = soapResponseDoc.createElementNS( "http://schemas.xmlsoap.org/soap/envelope/", "soap:Body" );
  soapEnvelopeElement.appendChild( soapBodyElement );

  QDomElement msExceptionsElement = soapResponseDoc.createElement( "Exception" );
  msExceptionsElement.setAttribute( "format", "text/xml" );
  soapBodyElement.appendChild( msExceptionsElement );

  QDomText msExceptionMessage = soapResponseDoc.createTextNode( QString( ex.message() ) );
  msExceptionsElement.appendChild( msExceptionMessage );

  QByteArray ba = soapResponseDoc.toByteArray();
  setHttpResponse( &ba, "text/xml" );
}
Esempio n. 7
0
int QgsSOAPRequestHandler::setUrlToFile( QImage* img )
{
  QString uri;
  QFile theFile;
  QDir tmpDir;

  QgsDebugMsg( "Entering." );

  if ( findOutHostAddress( uri ) != 0 )
  {
    return 1;
  }

#ifdef Q_OS_WIN
  if ( !QFile::exists( QDir::currentPath() + "/tmp" ) )
  {
    QDir::current().mkdir( "tmp" );
  }
  tmpDir = QDir( QDir::currentPath() + "/tmp" );
#else // Q_OS_WIN
  tmpDir = QDir( "/tmp" );
#endif

  QgsDebugMsg( "Path to tmpDir is: " + tmpDir.absolutePath() );

  //create a random folder under /tmp with map.jpg/png in it
  //and return the link to the client
  srand( time( nullptr ) );
  int randomNumber = rand();
  QString folderName = QString::number( randomNumber );
  if ( !QFile::exists( tmpDir.absolutePath() + "/mas_tmp" ) )
  {
    QgsDebugMsg( "Trying to create mas_tmp folder" );
    if ( !tmpDir.mkdir( "mas_tmp" ) )
    {
      return 2;
    }
  }
  QDir tmpMasDir( tmpDir.absolutePath() + "/mas_tmp" );
  if ( !tmpMasDir.mkdir( folderName ) )
  {
    QgsDebugMsg( "Trying to create random folder" );
    return 3;
  }

  QgsDebugMsg( "Temp. folder is: " + tmpMasDir.absolutePath() + "/" + folderName );

  if ( mFormat == "JPG" )
  {
    theFile.setFileName( tmpMasDir.absolutePath() + "/" + folderName + "/map.jpg" );
    uri.append( "/mas_tmp/" + folderName + "/map.jpg" );
  }
  else if ( mFormat == "PNG" )
  {
    theFile.setFileName( tmpMasDir.absolutePath() + "/" + folderName + "/map.png" );
    uri.append( "/mas_tmp/" + folderName + "/map.png" );
  }
  if ( !theFile.open( QIODevice::WriteOnly ) )
  {
    QgsDebugMsg( "Error, could not open file" );
    return 4;
  }

  if ( !img->save( &theFile, mFormat.toLocal8Bit().data(), -1 ) )
  {
    QgsDebugMsg( "Error, could not save image" );
    return 5;
  }

  QDomDocument xmlResponse;
  QDomElement envelopeElement = xmlResponse.createElementNS( "http://schemas.xmlsoap.org/soap/envelope/", "Envelope" );
  xmlResponse.appendChild( envelopeElement );
  QDomElement bodyElement = xmlResponse.createElementNS( "http://schemas.xmlsoap.org/soap/envelope/", "Body" );
  envelopeElement.appendChild( bodyElement );
  QDomElement getMapResponseElement = xmlResponse.createElementNS( "http://www.eu-orchestra.org/services/ms", "getMapResponse" );
  QDomElement ahrefElement = xmlResponse.createElement( "a" );
  ahrefElement.setAttribute( "href", uri );
  //QString href("<a href=\""+uri+"\">"+uri+"</a>");
  QDomText uriNode = xmlResponse.createTextNode( uri );
  ahrefElement.appendChild( uriNode );
  //getMapResponseElement.appendChild(uriNode);
  getMapResponseElement.appendChild( ahrefElement );
  bodyElement.appendChild( getMapResponseElement );

  QByteArray xmlByteArray = xmlResponse.toByteArray();
  setHttpResponse( &xmlByteArray, "text/xml" );
  return 0;
}
Esempio n. 8
0
void QgsSOAPRequestHandler::setGetCapabilitiesResponse( const QDomDocument& doc )
{
  //Parse the QDomDocument Document and create a SOAP response
  QDomElement DocCapabilitiesElement = doc.firstChildElement();
  if ( !DocCapabilitiesElement.isNull() )
  {
    QDomNodeList capabilitiesNodes =  DocCapabilitiesElement.elementsByTagName( "Capability" );
    if ( !capabilitiesNodes.isEmpty() )
    {

      //create response document
      QDomDocument soapResponseDoc;
      //Envelope element
      QDomElement soapEnvelopeElement = soapResponseDoc.createElement( "soap:Envelope" );
      soapEnvelopeElement.setAttribute( "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/" );
      soapEnvelopeElement.setAttribute( "soap:encoding", "http://schemas.xmlsoap.org/soap/encoding/" );
      soapResponseDoc.appendChild( soapEnvelopeElement );

      //Body element
      QDomElement soapBodyElement = soapResponseDoc.createElementNS( "http://schemas.xmlsoap.org/soap/envelope/", "soap:Body" );
      soapEnvelopeElement.appendChild( soapBodyElement );

      // check if WMS or MS SOAP request

      if ( mService == "MS" || mService == "MDS" || mService == "MAS" )
      {
        //OA_MI_MS_Capabilities element
        QDomElement msCapabilitiesElement = soapResponseDoc.createElement( "ms:OA_MI_Service_Capabilities" );
        msCapabilitiesElement.setAttribute( "service", "MS" );
        msCapabilitiesElement.setAttribute( "version", "1.1" );
        msCapabilitiesElement.setAttribute( "xmlns:ms", "http://www.eu-orchestra.org/services/ms" );
        msCapabilitiesElement.setAttribute( "xmlns", "http://www.eu-orchestra.org/services/oas/oa_basic" );
        msCapabilitiesElement.setAttribute( "xmlns:gml", "http://www.opengis.net/gml" );
        msCapabilitiesElement.setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
        msCapabilitiesElement.setAttribute( "xmlns:xsd", "http://www.w3.org/2001/XMLSchema" );
        soapBodyElement.appendChild( msCapabilitiesElement );

        // import the orchestra common capabilities
        QFile common( "common.xml" );
        if ( !common.open( QIODevice::ReadOnly ) )
        {
          //throw an exception...
          QgsDebugMsg( "external orchestra common capabilities not found" );
        }
        else
        {
          QDomDocument externCapDoc;
          QString parseError;
          int errorLineNo;
          if ( !externCapDoc.setContent( &common, false, &parseError, &errorLineNo ) )
          {
            QgsDebugMsg( "parse error at setting content of external orchestra common capabilities: "
                         + parseError + " at line " + QString::number( errorLineNo ) );
            common.close();
          }
          common.close();

          // write common capabilities
          QDomElement orchestraCommon = externCapDoc.firstChildElement();
          msCapabilitiesElement.appendChild( orchestraCommon );

          // write specific capabilities
          QDomElement msSpecificCapabilitiesElement = soapResponseDoc.createElement( "serviceSpecificCapabilities" );
          soapBodyElement.appendChild( msSpecificCapabilitiesElement );

          QDomElement capabilitiesElement = capabilitiesNodes.item( 0 ).toElement();
          msSpecificCapabilitiesElement.appendChild( capabilitiesElement );

          // to do supportedOperations
          QDomNodeList requestNodes = capabilitiesElement.elementsByTagName( "Request" );
          if ( !requestNodes.isEmpty() )
          {
            QDomElement requestElement = requestNodes.item( 0 ).toElement();
            QDomNodeList requestChildNodes = requestElement.childNodes();

            //append an array element for 'supportedOperations' to the soap document
            QDomElement supportedOperationsElement = soapResponseDoc.createElement( "supportedOperations" );
            supportedOperationsElement.setAttribute( "xsi:type", "soapenc:Array" );
            supportedOperationsElement.setAttribute( "soap:arrayType", "xsd:string[" + QString::number( requestChildNodes.size() ) + "]" );

            for ( int i = 0; i < requestChildNodes.size(); ++i )
            {
              QDomElement itemElement = soapResponseDoc.createElement( "item" );
              QDomText itemText = soapResponseDoc.createTextNode( requestChildNodes.item( i ).toElement().tagName() );
              itemElement.appendChild( itemText );
              supportedOperationsElement.appendChild( itemElement );
            }
          }
          //operationTypeInfo
          //predefinedLayers
          //predefinedDimensions
          //layerTypeInfo
          //predefinedStyles
          //supportedLayerDataInputFormat
          //supportedExceptionFormats
          //supportedDCP
        }


      }
      else if ( mService == "WMS" )
      {
        //WMS_Capabilities element
        QDomElement msCapabilitiesElement = soapResponseDoc.createElement( "wms:Capabilities" );
        msCapabilitiesElement.setAttribute( "service", "WMS" );
        msCapabilitiesElement.setAttribute( "version", "1.3.0" );
        msCapabilitiesElement.setAttribute( "xmlns:wms", "http://www.opengis.net/wms" );
        msCapabilitiesElement.setAttribute( "xmlns:gml", "http://www.opengis.net/gml" );
        msCapabilitiesElement.setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
        msCapabilitiesElement.setAttribute( "xmlns:xsd", "http://www.w3.org/2001/XMLSchema" );
        soapBodyElement.appendChild( msCapabilitiesElement );

        QFile wmsService( "wms_metadata.xml" );
        if ( !wmsService.open( QIODevice::ReadOnly ) )
        {
          //throw an exception...
          QgsDebugMsg( "external wms service capabilities not found" );
        }
        else
        {
          QDomDocument externServiceDoc;
          QString parseError;
          int errorLineNo;
          if ( !externServiceDoc.setContent( &wmsService, false, &parseError, &errorLineNo ) )
          {
            QgsDebugMsg( "parse error at setting content of external wms service capabilities: "
                         + parseError + " at line " + QString::number( errorLineNo ) );
            wmsService.close();
          }
          wmsService.close();

          // write WMS Service capabilities
          QDomElement service = externServiceDoc.firstChildElement();
          msCapabilitiesElement.appendChild( service );
        }

        QDomElement msServiceElement = soapResponseDoc.createElement( "Service" );
        msCapabilitiesElement.appendChild( msServiceElement );

        QDomElement capabilitiesElement = capabilitiesNodes.item( 0 ).toElement();
        msCapabilitiesElement.appendChild( capabilitiesElement );



      }
      else
      {
        //OA_MI_MS_Capabilities element
        QDomElement msCapabilitiesElement = soapResponseDoc.createElement( "ms:OA_MI_Service_Capabilities" );
        msCapabilitiesElement.setAttribute( "service", "MS" );
        msCapabilitiesElement.setAttribute( "version", "1.1" );
        msCapabilitiesElement.setAttribute( "xmlns:ms", "http://www.eu-orchestra.org/services/ms" );
        msCapabilitiesElement.setAttribute( "xmlns", "http://www.eu-orchestra.org/services/oas/oa_basic" );
        msCapabilitiesElement.setAttribute( "xmlns:gml", "http://www.opengis.net/gml" );
        msCapabilitiesElement.setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
        msCapabilitiesElement.setAttribute( "xmlns:xsd", "http://www.w3.org/2001/XMLSchema" );
        soapBodyElement.appendChild( msCapabilitiesElement );

        // import the orchestra common capabilities
        QFile common( "common.xml" );
        if ( !common.open( QIODevice::ReadOnly ) )
        {
          //throw an exception...
          QgsDebugMsg( "external orchestra common capabilities not found" );
        }
        else
        {
          QDomDocument externCapDoc;
          QString parseError;
          int errorLineNo;
          if ( !externCapDoc.setContent( &common, false, &parseError, &errorLineNo ) )
          {
            QgsDebugMsg( "parse error at setting content of external orchestra common capabilities: "
                         + parseError + " at line " + QString::number( errorLineNo ) );
            common.close();
          }
          common.close();

          // write common capabilities
          QDomElement orchestraCommon = externCapDoc.firstChildElement();
          msCapabilitiesElement.appendChild( orchestraCommon );
        }
      }

      QByteArray ba = soapResponseDoc.toByteArray();
      setHttpResponse( &ba, "text/xml" );
    }
  }
}
Esempio n. 9
0
void QgsRequestHandler::setXmlResponse( const QDomDocument& doc, const QString& mimeType )
{
  QByteArray ba = doc.toByteArray();
  setHttpResponse( ba, mimeType );
}
Esempio n. 10
0
void QgsRequestHandler::setXmlResponse( const QDomDocument& doc )
{
  QByteArray ba = doc.toByteArray();
  setHttpResponse( ba, QStringLiteral( "text/xml" ) );
}
Esempio n. 11
0
void QgsHttpRequestHandler::setGetCoverageResponse( QByteArray* ba )
{
  setHttpResponse( ba, "image/tiff" );
}
Esempio n. 12
0
void QgsHttpRequestHandler::setGetFeatureInfoResponse( const QDomDocument& infoDoc, const QString& infoFormat )
{
  QByteArray ba;
  QgsMessageLog::logMessage( "Info format is:" + infoFormat );

  if ( infoFormat == "text/xml" || infoFormat.startsWith( "application/vnd.ogc.gml" ) )
  {
    ba = infoDoc.toByteArray();
  }
  else if ( infoFormat == "text/plain" || infoFormat == "text/html" )
  {
    //create string
    QString featureInfoString;

    if ( infoFormat == "text/plain" )
    {
      featureInfoString.append( "GetFeatureInfo results\n" );
      featureInfoString.append( "\n" );
    }
    else if ( infoFormat == "text/html" )
    {
      featureInfoString.append( "<HEAD>\n" );
      featureInfoString.append( "<TITLE> GetFeatureInfo results </TITLE>\n" );
      featureInfoString.append( "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n" );
      featureInfoString.append( "</HEAD>\n" );
      featureInfoString.append( "<BODY>\n" );
    }

    QDomNodeList layerList = infoDoc.elementsByTagName( "Layer" );

    //layer loop
    for ( int i = 0; i < layerList.size(); ++i )
    {
      QDomElement layerElem = layerList.at( i ).toElement();
      if ( infoFormat == "text/plain" )
      {
        featureInfoString.append( "Layer '" + layerElem.attribute( "name" ) + "'\n" );
      }
      else if ( infoFormat == "text/html" )
      {
        featureInfoString.append( "<TABLE border=1 width=100%>\n" );
        featureInfoString.append( "<TR><TH width=25%>Layer</TH><TD>" + layerElem.attribute( "name" ) + "</TD></TR>\n" );
        featureInfoString.append( "</BR>" );
      }

      //feature loop (for vector layers)
      QDomNodeList featureNodeList = layerElem.elementsByTagName( "Feature" );
      QDomElement currentFeatureElement;

      if ( featureNodeList.size() < 1 ) //raster layer?
      {
        QDomNodeList attributeNodeList = layerElem.elementsByTagName( "Attribute" );
        for ( int j = 0; j < attributeNodeList.size(); ++j )
        {
          QDomElement attributeElement = attributeNodeList.at( j ).toElement();
          if ( infoFormat == "text/plain" )
          {
            featureInfoString.append( attributeElement.attribute( "name" ) + " = '" +
                                      attributeElement.attribute( "value" ) + "'\n" );
          }
          else if ( infoFormat == "text/html" )
          {
            featureInfoString.append( "<TR><TH>" + attributeElement.attribute( "name" ) + "</TH><TD>" +
                                      attributeElement.attribute( "value" ) + "</TD></TR>\n" );
          }
        }
      }
      else //vector layer
      {
        for ( int j = 0; j < featureNodeList.size(); ++j )
        {
          QDomElement featureElement = featureNodeList.at( j ).toElement();
          if ( infoFormat == "text/plain" )
          {
            featureInfoString.append( "Feature " + featureElement.attribute( "id" ) + "\n" );
          }
          else if ( infoFormat == "text/html" )
          {
            featureInfoString.append( "<TABLE border=1 width=100%>\n" );
            featureInfoString.append( "<TR><TH>Feature</TH><TD>" + featureElement.attribute( "id" ) + "</TD></TR>\n" );
          }
          //attribute loop
          QDomNodeList attributeNodeList = featureElement.elementsByTagName( "Attribute" );
          for ( int k = 0; k < attributeNodeList.size(); ++k )
          {
            QDomElement attributeElement = attributeNodeList.at( k ).toElement();
            if ( infoFormat == "text/plain" )
            {
              featureInfoString.append( attributeElement.attribute( "name" ) + " = '" +
                                        attributeElement.attribute( "value" ) + "'\n" );
            }
            else if ( infoFormat == "text/html" )
            {
              featureInfoString.append( "<TR><TH>" + attributeElement.attribute( "name" ) + "</TH><TD>" + attributeElement.attribute( "value" ) + "</TD></TR>\n" );
            }
          }

          if ( infoFormat == "text/html" )
          {
            featureInfoString.append( "</TABLE>\n</BR>\n" );
          }
        }
      }
      if ( infoFormat == "text/plain" )
      {
        featureInfoString.append( "\n" );
      }
      else if ( infoFormat == "text/html" )
      {
        featureInfoString.append( "</TABLE>\n<BR></BR>\n" );

      }
    }
    if ( infoFormat == "text/html" )
    {
      featureInfoString.append( "</BODY>\n" );
    }
    ba = featureInfoString.toUtf8();
  }
  else //unsupported format, set exception
  {
    setServiceException( QgsMapServiceException( "InvalidFormat", "Feature info format '" + mFormat + "' is not supported. Possibilities are 'text/plain', 'text/html' or 'text/xml'." ) );
    return;
  }

  setHttpResponse( &ba, infoFormat );
}
Esempio n. 13
0
void QgsHttpRequestHandler::setXmlResponse( const QDomDocument& doc )
{
  QByteArray ba = doc.toByteArray();
  setHttpResponse( &ba, "text/xml" );
}