String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE, "text/html"); response.setContentType(mimeType); writeResponse(response, result, encoding);
if(!response.isCommitted()) { String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE, "text/html"); response.setContentType(mimeType); writeResponse(response, result, encoding); }
    public void doGet(DBBroker broker, HttpServletRequest request, HttpServletResponse response, String path)    throws BadRequestException, PermissionDeniedException,            NotFoundException, IOException {    	    	//if required, set character encoding    	if (request.getCharacterEncoding() == null)			request.setCharacterEncoding(formEncoding);    	        // Process special parameters                int howmany = 10;        int start = 1;        boolean wrap = true;        boolean source = false;        Properties outputProperties = new Properties();        String query = request.getParameter("_xpath");        if (query == null)            query = request.getParameter("_query");                String p_howmany = request.getParameter("_howmany");        if (p_howmany != null) {            try {                howmany = Integer.parseInt(p_howmany);            } catch (NumberFormatException nfe) {                throw new BadRequestException(                        "Parameter _howmany should be an int");            }        }        String p_start = (String) request.getParameter("_start");        if (p_start != null) {            try {                start = Integer.parseInt(p_start);            } catch (NumberFormatException nfe) {                throw new BadRequestException(                        "Parameter _start should be an int");            }        }        String option;        if ((option = request.getParameter("_wrap")) != null)            wrap = option.equals("yes");        if ((option = request.getParameter("_indent")) != null)            outputProperties.setProperty(OutputKeys.INDENT, option);        if((option = request.getParameter("_source")) != null)        	source = option.equals("yes");        String stylesheet;        if ((stylesheet = request.getParameter("_xsl")) != null) {            if (stylesheet.equals("no")) {                outputProperties.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "no");                outputProperties.remove(EXistOutputKeys.STYLESHEET);                stylesheet = null;            } else                outputProperties.setProperty(EXistOutputKeys.STYLESHEET,                        stylesheet);        } else            outputProperties.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "yes");        LOG.debug("stylesheet = " + stylesheet);        LOG.debug("query = " + query);        String encoding;        if ((encoding = request.getParameter("_encoding")) != null)            outputProperties.setProperty(OutputKeys.ENCODING, encoding);        else            encoding = "UTF-8";                // Process the request        DocumentImpl resource = null;        XmldbURI pathUri = XmldbURI.create(path);        try {            // check if path leads to an XQuery resource            resource = broker.getXMLResource(pathUri, Lock.READ_LOCK);            if (resource != null)            {                if (resource.getResourceType() == DocumentImpl.BINARY_FILE && "application/xquery".equals(resource.getMetadata().getMimeType()))                {                	// found an XQuery resource                                    	//Should we display the source of the XQuery or execute it                	Descriptor descriptor = Descriptor.getDescriptorSingleton();                	if(source && descriptor != null)                	{                		//show the source                		                		//check are we allowed to show the xquery source - descriptor.xml                		if(descriptor.allowSourceXQuery(path))                		{                			//TODO: change writeResourceAs to use a serializer that will serialize xquery to syntax coloured xhtml, replace the asMimeType parameter with a method for specifying the serializer, or split the code into two methods. - deliriumsky                			                			//Show the source of the XQuery                			writeResourceAs(resource, broker, stylesheet, encoding, "text/plain", outputProperties, response);                		}                		else                		{                			//we are not allowed to show the source - query not allowed in descriptor.xml                			//TODO: is this the correct exception to throw or should we return a http response?                			throw new PermissionDeniedException("Permission to view XQuery source for: " + path + " denied. Must be explicitly defined in descriptor.xml");                		}                		                	}                	else            		{            			//Execute the XQuery            			try						{                            String result = executeXQuery(broker, resource, request, response, outputProperties);                            encoding = outputProperties.getProperty(OutputKeys.ENCODING, encoding);                            String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE, "text/html");                            if (!response.isCommitted())                                response.setContentType(mimeType);                            writeResponse(response, result, encoding);                        }            			catch (XPathException e)						{                            response.setContentType("text/html");                            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                            writeResponse(response, formatXPathException(query, path, e), encoding);                        }            		}                    return;                }            }            if (query != null) {                // query parameter specified                try {                    String result = search(broker, query, path, howmany, start, outputProperties, wrap, request, response);                    encoding = outputProperties.getProperty(OutputKeys.ENCODING, encoding);                    String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE, "text/html");                    response.setContentType(mimeType);                    writeResponse(response, result, encoding);                } catch (XPathException e) {                    response.setContentType("text/html");                    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                    writeResponse(response, formatXPathException(query, path, e), encoding);                }            } else {                // no query parameter: try to load a document from the specified                // path                if (resource == null) {                    // no document: check if path points to a collection                    Collection collection = broker.getCollection(pathUri);                    if (collection != null) {                        if (!collection.getPermissions().validate(                                broker.getUser(), Permission.READ))                            throw new PermissionDeniedException(                                    "Not allowed to read collection");                        // return a listing of the collection contents                        response.setContentType("text/xml");                        writeResponse(response, printCollection(broker, collection), encoding);                    } else {                        throw new NotFoundException("Document " + path                                + " not found");                    }                } else {                    // document found: serialize it                    writeResourceAs(resource, broker, stylesheet, encoding, null, outputProperties, response);                }            }        } finally {            if (resource != null)                resource.getUpdateLock().release(Lock.READ_LOCK);        }    }