| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 4 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 5 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 10 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 11 |
* contributor license agreements. See the NOTICE file distributed |
| 12 |
* with this work for additional information regarding copyright |
| 13 |
* ownership. The ASF licenses this file to you under the Apache |
| 14 |
* License, Version 2.0 (the "License"); you may not use this file |
| 15 |
* except in compliance with the License. You may obtain a copy of |
| 16 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package com.sun.star.wiki; |
| 20 |
|
| 21 |
import java.io.StringReader; |
| 22 |
import java.io.OutputStreamWriter; |
| 23 |
import java.util.Map; |
| 24 |
import java.net.URLEncoder; |
| 25 |
import java.net.URI; |
| 26 |
import java.net.HttpURLConnection; |
| 27 |
|
| 28 |
import javax.swing.text.html.HTMLEditorKit; |
| 29 |
|
| 30 |
import com.sun.star.uno.XComponentContext; |
| 31 |
|
| 32 |
|
| 33 |
public class WikiArticle |
| 34 |
{ |
| 35 |
private final XComponentContext m_xContext; |
| 36 |
|
| 37 |
private String m_sEditTime = ""; |
| 38 |
private String m_sEditToken = ""; |
| 39 |
|
| 40 |
private String m_sHTMLCode; |
| 41 |
private boolean m_bNoArticle = true; |
| 42 |
|
| 43 |
private String m_sWikiUser; |
| 44 |
private String m_sWikiPass; |
| 45 |
|
| 46 |
private final String m_sTitle; |
| 47 |
|
| 48 |
private final URI m_aMainURI; |
| 49 |
private boolean m_isLoggedIn = false; |
| 50 |
|
| 51 |
/** Creates a new instance of WikiArticle */ |
| 52 |
public WikiArticle( XComponentContext xContext, String sTitle, Map<String,String> wikiSettings, boolean bLogin, WikiPropDialog aPropDialog ) |
| 53 |
throws java.net.URISyntaxException, java.io.IOException, WikiCancelException |
| 54 |
{ |
| 55 |
m_xContext = xContext; |
| 56 |
|
| 57 |
String sMainUrl = wikiSettings.get("Url"); |
| 58 |
m_sWikiUser = wikiSettings.get("Username"); |
| 59 |
m_sWikiPass = wikiSettings.get("Password"); |
| 60 |
m_sTitle = sTitle; |
| 61 |
|
| 62 |
m_aMainURI = new URI(sMainUrl); |
| 63 |
|
| 64 |
if ( bLogin ) |
| 65 |
{ |
| 66 |
WikiEditSettingDialog aDialog = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application", wikiSettings, false ); |
| 67 |
try |
| 68 |
{ |
| 69 |
while( !Login() ) |
| 70 |
{ |
| 71 |
if ( aPropDialog != null ) |
| 72 |
aPropDialog.SetThrobberActive( false ); |
| 73 |
|
| 74 |
if ( MainThreadDialogExecutor.Show( xContext, aDialog ) ) |
| 75 |
{ |
| 76 |
m_sWikiUser = wikiSettings.get("Username"); |
| 77 |
m_sWikiPass = wikiSettings.get("Password"); |
| 78 |
} |
| 79 |
else |
| 80 |
throw new WikiCancelException(); |
| 81 |
|
| 82 |
if ( aPropDialog != null ) |
| 83 |
{ |
| 84 |
aPropDialog.SetThrobberActive( true ); |
| 85 |
Thread.yield(); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
finally |
| 90 |
{ |
| 91 |
aDialog.DisposeDialog(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// in case of loading the html contents are used |
| 96 |
// in case of saving the contents should be checked whether they are empty |
| 97 |
InitArticleHTML(); |
| 98 |
} |
| 99 |
|
| 100 |
public String GetMainURL() |
| 101 |
{ |
| 102 |
return m_aMainURI.toString(); |
| 103 |
} |
| 104 |
|
| 105 |
public String GetTitle() |
| 106 |
{ |
| 107 |
return m_sTitle; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private String getArticleWiki() |
| 113 |
throws java.net.URISyntaxException, java.io.IOException, WikiCancelException |
| 114 |
{ |
| 115 |
String sWikiCode = null; |
| 116 |
|
| 117 |
if (m_isLoggedIn) |
| 118 |
{ |
| 119 |
URI aURI = new URI(m_aMainURI.toString() + "index.php?title=" + m_sTitle + "&action=edit"); |
| 120 |
HttpURLConnection connGet = Helper.PrepareMethod("GET", aURI, m_xContext); |
| 121 |
connGet.connect(); |
| 122 |
|
| 123 |
int nResultCode = connGet.getResponseCode(); |
| 124 |
String sWebPage = null; |
| 125 |
if (nResultCode == 200) { |
| 126 |
sWebPage = Helper.ReadResponseBody(connGet); |
| 127 |
} |
| 128 |
|
| 129 |
if ( sWebPage != null ) |
| 130 |
{ |
| 131 |
StringReader r = new StringReader(sWebPage); |
| 132 |
HTMLEditorKit.Parser parse = Helper.GetHTMLParser(); |
| 133 |
EditPageParser callback = new EditPageParser(); |
| 134 |
|
| 135 |
parse.parse(r,callback,true); |
| 136 |
m_sEditTime = callback.m_sEditTime; |
| 137 |
m_sEditToken = callback.m_sEditToken; |
| 138 |
|
| 139 |
int iPosStart = callback.m_nWikiArticleStart; |
| 140 |
int iPosEnd = callback.m_nWikiArticleEnd; |
| 141 |
|
| 142 |
if ( iPosStart >= 0 && iPosEnd > 0 ) |
| 143 |
{ |
| 144 |
String sArticle = sWebPage.substring(iPosStart, iPosEnd); |
| 145 |
iPosStart = sArticle.indexOf('>') + 1; |
| 146 |
sWikiCode = sArticle.substring( iPosStart, sArticle.length() ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return sWikiCode; |
| 152 |
} |
| 153 |
|
| 154 |
private void InitArticleHTML() |
| 155 |
throws java.net.URISyntaxException, java.io.IOException, WikiCancelException |
| 156 |
{ |
| 157 |
if (m_isLoggedIn) |
| 158 |
{ |
| 159 |
URI uri = new URI(m_aMainURI.toString() + "index.php?title=" + m_sTitle); |
| 160 |
HttpURLConnection connGet = Helper.PrepareMethod("GET", uri, m_xContext); |
| 161 |
connGet.connect(); |
| 162 |
|
| 163 |
int nResultCode = connGet.getResponseCode(); |
| 164 |
String sWebPage = null; |
| 165 |
if (nResultCode == 200) { |
| 166 |
sWebPage = Helper.ReadResponseBody(connGet); |
| 167 |
} |
| 168 |
|
| 169 |
if ( sWebPage != null ) |
| 170 |
{ |
| 171 |
StringReader r = new StringReader(sWebPage); |
| 172 |
HTMLEditorKit.Parser parse = Helper.GetHTMLParser(); |
| 173 |
EditPageParser callback = new EditPageParser(); |
| 174 |
|
| 175 |
parse.parse(r,callback,true); |
| 176 |
|
| 177 |
int iPosStart = callback.m_nHTMLArticleStart; |
| 178 |
int iPosEnd = callback.m_nHTMLArticleEnd; |
| 179 |
int nPosNoArt = callback.m_nNoArticleInd; |
| 180 |
|
| 181 |
if ( iPosStart >= 0 && iPosEnd > 0 ) |
| 182 |
{ |
| 183 |
m_sHTMLCode = sWebPage.substring(iPosStart, iPosEnd); |
| 184 |
m_bNoArticle = ( nPosNoArt >= 0 && nPosNoArt >= iPosStart && nPosNoArt <= iPosEnd ); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
protected boolean setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit ) |
| 191 |
throws java.net.URISyntaxException, java.io.IOException, WikiCancelException |
| 192 |
{ |
| 193 |
boolean bResult = false; |
| 194 |
|
| 195 |
if (m_isLoggedIn && sWikiCode != null && sWikiComment != null) |
| 196 |
{ |
| 197 |
// get the edit time and token |
| 198 |
getArticleWiki(); |
| 199 |
|
| 200 |
URI uri = new URI(m_aMainURI.toString() + "index.php?title=" + m_sTitle + "&action=submit"); |
| 201 |
|
| 202 |
HttpURLConnection connPost = Helper.PrepareMethod("POST", uri, m_xContext); |
| 203 |
connPost.setDoInput(true); |
| 204 |
connPost.setDoOutput(true); |
| 205 |
connPost.connect(); |
| 206 |
|
| 207 |
OutputStreamWriter post = new OutputStreamWriter(connPost.getOutputStream(), "UTF-8"); |
| 208 |
try |
| 209 |
{ |
| 210 |
post.write("wpTextbox1="); |
| 211 |
post.write(URLEncoder.encode(sWikiCode, "UTF-8")); |
| 212 |
post.write("&wpSummary="); |
| 213 |
post.write(URLEncoder.encode(sWikiComment, "UTF-8")); |
| 214 |
post.write("&wpSection="); |
| 215 |
post.write("&wpEdittime="); |
| 216 |
post.write(URLEncoder.encode(m_sEditTime, "UTF-8")); |
| 217 |
post.write("&wpSave=Save%20page"); |
| 218 |
post.write("&wpEditToken="); |
| 219 |
post.write(URLEncoder.encode(m_sEditToken, "UTF-8")); |
| 220 |
|
| 221 |
if (bMinorEdit) { |
| 222 |
post.write("&wpMinoredit=1"); |
| 223 |
} |
| 224 |
|
| 225 |
post.flush(); |
| 226 |
} |
| 227 |
finally |
| 228 |
{ |
| 229 |
post.close(); |
| 230 |
} |
| 231 |
|
| 232 |
int nResultCode = connPost.getResponseCode(); |
| 233 |
if ( nResultCode < 400 ) |
| 234 |
bResult = true; |
| 235 |
|
| 236 |
String aResult = Helper.ReadResponseBody(connPost); |
| 237 |
|
| 238 |
// TODO: remove the debug printing, try to detect the error |
| 239 |
System.out.print( "nSubmitCode = " + nResultCode + "\n===\n" + aResult ); |
| 240 |
} |
| 241 |
|
| 242 |
return bResult; |
| 243 |
} |
| 244 |
|
| 245 |
private boolean Login() |
| 246 |
throws java.net.URISyntaxException, java.io.IOException, WikiCancelException |
| 247 |
{ |
| 248 |
m_isLoggedIn = Helper.Login( m_aMainURI, m_sWikiUser, m_sWikiPass, m_xContext ); |
| 249 |
return m_isLoggedIn; |
| 250 |
} |
| 251 |
|
| 252 |
protected boolean NotExist() |
| 253 |
{ |
| 254 |
boolean bResult = true; |
| 255 |
if ( m_sHTMLCode != null ) |
| 256 |
bResult = m_bNoArticle; |
| 257 |
|
| 258 |
return bResult; |
| 259 |
} |
| 260 |
|
| 261 |
} |