Long createOrUpdatePage()

in src/main/groovy/com/epam/esp/confluence/ConfluenceHelper.groovy [100:139]


    Long createOrUpdatePage(String spaceKey, Long parentPageId, String title, String pageBody,
                            EditorVersion editorVersion = EditorVersion.V2,
                            boolean fullWidth = false,
                            boolean failIfExist = false) {
        HttpEntity putPageEntity = null
        try {
            def existPageId = findPageIdByTitle(spaceKey, title)
            if (existPageId != null) {
                if (failIfExist) {
                    def errorMessage = "Page with title '${title}' already exists in space ${spaceKey} with id ${existPageId}"
                    logger.error(errorMessage)
                    throw new ConfluenceException(errorMessage)
                }
                updatePage(existPageId, pageBody)
                return existPageId
            }
            HttpPost putPageRequest = new HttpPost(createContentRestUrl())
            def pageDto = new NewConfPageDto(spaceKey, parentPageId, title, pageBody, editorVersion, fullWidth)
            StringEntity entity = new StringEntity(JsonMapper.getInstance().map(pageDto), ContentType.APPLICATION_JSON)
            putPageRequest.setEntity(entity)
            HttpResponse putPageResponse = client.execute(putPageRequest)
            putPageEntity = putPageResponse.getEntity()
            logger.info(putPageRequest.requestLine.uri)
            def pageObj = IOUtils.toString(putPageEntity.getContent())
            if (putPageResponse.statusLine.statusCode != 200) {
                logger.error("Put Page Request returned ${putPageResponse.statusLine}")
                logger.error(pageObj)
                throw new ConfluenceException("Unexpected API response code")
            } else {
                logger.info("PUT Page Request returned " + putPageResponse.statusLine.toString())
                JSONObject jsonPage = new JSONObject(pageObj)
                def url = jsonPage.getJSONObject('_links').getString('base') + jsonPage.getJSONObject('_links').getString('tinyui')
                logger.info("page URL: $url")
                return jsonPage.getLong("id")
            }
        }
        finally {
            EntityUtils.consume(putPageEntity)
        }
    }