public String createNode()

in src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java [143:215]


    public String createNode(String url, NameValuePairList clientNodeProperties, Map<String,String> requestHeaders, boolean multiPart, 
    		File localFile, String fieldName, String typeHint) 
    throws IOException {
    	
        final PostMethod post = new PostMethod(url);
        post.setFollowRedirects(false);

        // create a private copy of the properties to not tamper with
        // the properties of the client
        NameValuePairList nodeProperties = new NameValuePairList(clientNodeProperties);

        // add sling specific properties
        nodeProperties.prependIfNew(":redirect", "*");
        nodeProperties.prependIfNew(":displayExtension", "");
        nodeProperties.prependIfNew(":status", "browser");

        // add fake property - otherwise the node is not created
        if (clientNodeProperties == null) {
            nodeProperties.add("jcr:created", "");
        }

        // force form encoding to UTF-8, which is what we use to convert the
        // string parts into stream data
        nodeProperties.addOrReplace("_charset_", "UTF-8");

        if( nodeProperties.size() > 0) {
            if(multiPart) {
                final List<Part> partList = new ArrayList<Part>();
                for(NameValuePair e : nodeProperties) {
                    if (e.getValue() != null) {
                        partList.add(new StringPart(e.getName(), e.getValue(), "UTF-8"));
                    }
                }
                if  (localFile != null) {
                    partList.add(new FilePart(fieldName, localFile));
                    if (typeHint != null) {
                    	partList.add(new StringPart(fieldName + "@TypeHint", typeHint));
                    }
                }
                final Part [] parts = partList.toArray(new Part[partList.size()]);
                post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
            } else {
            	post.getParams().setContentCharset("UTF-8");
                for(NameValuePair e : nodeProperties) {
                    post.addParameter(e.getName(),e.getValue());
                }
            }
        }

        if(requestHeaders != null) {
            for(Map.Entry<String,String> e : requestHeaders.entrySet()) {
                post.addRequestHeader(e.getKey(), e.getValue());
            }
        }

        final int expected = 302;
        final int status = httpClient.executeMethod(post);
        if(status!=expected) {
            throw new HttpStatusCodeException(expected, status, "POST", url, HttpTestBase.getResponseBodyAsStream(post, 0));
        }
        String location = post.getResponseHeader("Location").getValue();
        post.releaseConnection();
        // simple check if host is missing
        if (!location.startsWith("http://")) {
            String host = HttpTestBase.HTTP_BASE_URL;
            int idx = host.indexOf('/', 8);
            if (idx > 0) {
                host = host.substring(0, idx);
            }
            location = host + location;
        }
        return location;
    }