//---------------------------------------------------------------------------- MediaType MediaFoundationVideoDevice::GetFormat(unsigned int streamIndex, unsigned int formatIndex) const { if(streamIndex >= this->CurrentFormats.size()) { return MediaType(); } if (formatIndex >= this->CurrentFormats[streamIndex].size()) { return MediaType(); } return this->CurrentFormats[streamIndex][formatIndex]; }
void Config::request(ServerRequest::ptr request, Access access) { const std::string &method = request->request().requestLine.method; if (method == POST) { if (access != READWRITE) { respondError(request, FORBIDDEN); return; } if (request->request().entity.contentType.type != "application" || request->request().entity.contentType.subtype != "x-www-form-urlencoded") { respondError(request, UNSUPPORTED_MEDIA_TYPE); return; } Stream::ptr requestStream = request->requestStream(); requestStream.reset(new LimitedStream(requestStream, 65536)); MemoryStream requestBody; transferStream(requestStream, requestBody); std::string queryString; queryString.resize(requestBody.buffer().readAvailable()); requestBody.buffer().copyOut(&queryString[0], requestBody.buffer().readAvailable()); bool failed = false; URI::QueryString qs(queryString); for (URI::QueryString::const_iterator it = qs.begin(); it != qs.end(); ++it) { ConfigVarBase::ptr var = Mordor::Config::lookup(it->first); if (var && !var->fromString(it->second)) failed = true; } if (failed) { respondError(request, HTTP::FORBIDDEN, "One or more new values were not accepted"); return; } // Fall through } if (method == GET || method == HEAD || method == POST) { Format format = HTML; URI::QueryString qs; if (request->request().requestLine.uri.queryDefined()) qs = request->request().requestLine.uri.queryString(); URI::QueryString::const_iterator it = qs.find("alt"); if (it != qs.end() && it->second == "json") format = JSON; // TODO: use Accept to indicate JSON switch (format) { case HTML: { request->response().status.status = OK; request->response().entity.contentType = MediaType("text", "html"); if (method == HEAD) { if (request->request().requestLine.ver == Version(1, 1) && isAcceptable(request->request().request.te, "chunked", true)) { request->response().general.transferEncoding.push_back("chunked"); } return; } Stream::ptr response = request->responseStream(); response.reset(new BufferedStream(response)); response->write("<html><body><table>\n", 20); Mordor::Config::visit(boost::bind(access == READWRITE ? &eachConfigVarHTMLWrite : &eachConfigVarHTML, _1, response)); response->write("</table></body></html>", 22); response->close(); break; } case JSON: { JSON::Object root; Mordor::Config::visit(boost::bind(&eachConfigVarJSON, _1, boost::ref(root))); std::ostringstream os; os << root; std::string str = os.str(); request->response().status.status = OK; request->response().entity.contentType = MediaType("application", "json"); request->response().entity.contentLength = str.size(); if (method != HEAD) { request->responseStream()->write(str.c_str(), str.size()); request->responseStream()->close(); } break; } default: MORDOR_NOTREACHED(); } } else { respondError(request, METHOD_NOT_ALLOWED); } }
// OPTIONAL: Set extra text content type void WebServiceServer::SetContentType(CString p_extension,CString p_contentType) { m_contentTypes.insert(std::make_pair(p_extension,MediaType(p_extension,p_contentType))); }
// Hotrod 2.8 with mediatype allows to store and retrieve entries in different data format // this can be done specifying the mediatype for the communication int main(int argc, char** argv) { ConfigurationBuilder builder; builder.protocolVersion(Configuration::PROTOCOL_VERSION_28); builder.addServer().host("127.0.0.1").port(11222); builder.balancingStrategyProducer(nullptr); RemoteCacheManager cacheManager(builder.build(), false); cacheManager.start(); std::cout << "Tests for CacheManager" << std::endl; Marshaller<std::string> *km = new JBasicMarshaller<std::string>(); Marshaller<std::string> *vm = new JBasicMarshaller<std::string>(); RemoteCache<std::string, std::string> cache = cacheManager.getCache<std::string, std::string>(km, &Marshaller<std::string>::destroy, vm, &Marshaller<std::string>::destroy, std::string("transcodingCache")); // Define a data format for json DataFormat<std::string, std::string> df; df.keyMediaType = MediaType(APPLICATION_UNKNOWN_TYPE); df.valueMediaType = MediaType(APPLICATION_JSON_TYPE); df.valueMarshaller.reset(new BasicMarshaller<std::string>()); RemoteCache<std::string, std::string> cacheJson = cache.withDataFormat(&df); // Define a data forma for jboss marshaller DataFormat<std::string, std::string> df1; df1.keyMediaType = MediaType(APPLICATION_JBOSS_MARSHALLING_TYPE); df1.valueMediaType = MediaType(APPLICATION_JBOSS_MARSHALLING_TYPE); RemoteCache<std::string, std::string> cacheJBoss = cache.withDataFormat(&df1); std::string k1("key13"); std::string k2("key14"); std::string v1("boron"); std::string v2("chlorine"); cache.clear(); // put cache.put(k1, v1); std::unique_ptr<std::string> rv(cache.get(k1)); assert_not_null("get returned null!", __LINE__, rv); if (rv->compare(v1)) { std::cerr << "get/put fail for " << k1 << " got " << *rv << " expected " << v1 << std::endl; return 1; } cache.put(k2, v2); std::unique_ptr<std::string> rv2(cache.get(k2)); assert_not_null("get returned null!", __LINE__, rv2); if (rv2->compare(v2)) { std::cerr << "get/put fail for " << k2 << " got " << *rv2 << " expected " << v2 << std::endl; return 1; } std::unique_ptr<std::string> rv2Json(cacheJson.get(k2)); assert_not_null("get returned null!", __LINE__, rv2Json); if (rv2Json->compare("\"chlorine\"")) { std::cerr << "get/put fail for " << k2 << " got " << *rv2 << " expected \"boron\"" << std::endl; return 1; } std::unique_ptr<std::string> rv2JBoss(cacheJBoss.get(k2)); assert_not_null("get returned null!", __LINE__, rv2JBoss); if (rv2JBoss->compare(v2)) { std::cerr << "get/put fail for " << k2 << " got " << *rv2JBoss << " expected " << v2 << std::endl; return 1; } }