fix #1841 close CurlResponse

This commit is contained in:
Shinsuke Sugaya 2018-09-21 06:34:30 +09:00
parent f618759616
commit 15c73f2be4
21 changed files with 124 additions and 122 deletions

View file

@ -137,7 +137,7 @@ public class EsApiManager extends BaseApiManager {
curlRequest.param(entry.getKey(), entry.getValue()[0]);
}
});
final CurlResponse curlResponse = curlRequest.onConnect((req, con) -> {
try (final CurlResponse curlResponse = curlRequest.onConnect((req, con) -> {
con.setDoOutput(true);
if (httpMethod != Method.GET) {
try (ServletInputStream in = request.getInputStream(); OutputStream out = con.getOutputStream()) {
@ -146,13 +146,14 @@ public class EsApiManager extends BaseApiManager {
throw new WebApiException(HttpServletResponse.SC_BAD_REQUEST, e);
}
}
}).execute();
}).execute()) {
try (ServletOutputStream out = response.getOutputStream(); InputStream in = curlResponse.getContentAsStream()) {
response.setStatus(curlResponse.getHttpStatusCode());
CopyUtil.copy(in, out);
} catch (final ClientAbortException e) {
logger.debug("Client aborts this request.", e);
try (ServletOutputStream out = response.getOutputStream(); InputStream in = curlResponse.getContentAsStream()) {
response.setStatus(curlResponse.getHttpStatusCode());
CopyUtil.copy(in, out);
} catch (final ClientAbortException e) {
logger.debug("Client aborts this request.", e);
}
} catch (final Exception e) {
if (e.getCause() instanceof ClientAbortException) {
logger.debug("Client aborts this request.", e);

View file

@ -214,9 +214,7 @@ public class AdminDictKuromojiAction extends FessAdminAction {
verifyTokenKeep(() -> downloadpage(form.dictId));
return kuromojiService.getKuromojiFile(form.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadKuromojiFile(GLOBAL), () -> downloadpage(form.dictId));

View file

@ -217,9 +217,7 @@ public class AdminDictMappingAction extends FessAdminAction {
verifyTokenKeep(() -> downloadpage(form.dictId));
return charMappingService.getCharMappingFile(form.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadMappingFile(GLOBAL), () -> downloadpage(form.dictId));

View file

@ -212,9 +212,7 @@ public class AdminDictProtwordsAction extends FessAdminAction {
verifyTokenKeep(() -> downloadpage(form.dictId));
return protwordsService.getProtwordsFile(form.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL), () -> downloadpage(form.dictId));

View file

@ -217,9 +217,7 @@ public class AdminDictStemmeroverrideAction extends FessAdminAction {
.getStemmerOverrideFile(form.dictId)
.map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
})
.orElseGet(

View file

@ -210,9 +210,7 @@ public class AdminDictStopwordsAction extends FessAdminAction {
verifyTokenKeep(() -> downloadpage(form.dictId));
return stopwordsService.getStopwordsFile(form.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadStopwordsFile(GLOBAL), () -> downloadpage(form.dictId));

View file

@ -217,9 +217,7 @@ public class AdminDictSynonymAction extends FessAdminAction {
verifyTokenKeep(() -> downloadpage(form.dictId));
return synonymService.getSynonymFile(form.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationError(messages -> messages.addErrorsFailedToDownloadSynonymFile(GLOBAL), () -> downloadpage(form.dictId));

View file

@ -136,9 +136,7 @@ public class ApiAdminDictKuromojiAction extends FessApiAdminAction {
validateApi(body, messages -> {});
return kuromojiService.getKuromojiFile(body.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));

View file

@ -136,9 +136,7 @@ public class ApiAdminDictMappingAction extends FessApiAdminAction {
validateApi(body, messages -> {});
return charMappingService.getCharMappingFile(body.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));

View file

@ -135,9 +135,7 @@ public class ApiAdminDictProtwordsAction extends FessApiAdminAction {
validateApi(body, messages -> {});
return protwordsService.getProtwordsFile(body.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));

View file

@ -142,9 +142,7 @@ public class ApiAdminDictStemmeroverrideAction extends FessApiAdminAction {
validateApi(body, messages -> {});
return stemmerOverrideService.getStemmerOverrideFile(body.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));

View file

@ -135,9 +135,7 @@ public class ApiAdminDictStopwordsAction extends FessApiAdminAction {
validateApi(body, messages -> {});
return stopwordsService.getStopwordsFile(body.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadStopwordsFile(GLOBAL));

View file

@ -136,9 +136,7 @@ public class ApiAdminDictSynonymAction extends FessApiAdminAction {
validateApi(body, messages -> {});
return synonymService.getSynonymFile(body.dictId).map(file -> {
return asStream(new File(file.getPath()).getName()).contentTypeOctetStream().stream(out -> {
try (InputStream inputStream = file.getInputStream()) {
out.write(inputStream);
}
file.writeOut(out);
});
}).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsFailedToDownloadProtwordsFile(GLOBAL));

View file

@ -15,6 +15,9 @@
*/
package org.codelibs.fess.dict;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
@ -22,7 +25,9 @@ import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.codelibs.curl.CurlResponse;
import org.dbflute.optional.OptionalEntity;
import org.lastaflute.web.servlet.request.stream.WrittenStreamOut;
public abstract class DictionaryFile<T extends DictionaryItem> {
protected DictionaryManager dictionaryManager;
@ -56,6 +61,13 @@ public abstract class DictionaryFile<T extends DictionaryItem> {
return this;
}
public void writeOut(WrittenStreamOut out) throws IOException {
try (final CurlResponse curlResponse = dictionaryManager.getContentResponse(this);
final InputStream inputStream = new BufferedInputStream(curlResponse.getContentAsStream())) {
out.write(inputStream);
}
}
public abstract String getType();
public abstract PagingList<T> selectList(int offset, int size);

View file

@ -17,7 +17,6 @@ package org.codelibs.fess.dict;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@ -110,12 +109,8 @@ public class DictionaryManager {
});
}
public InputStream getContentInputStream(final DictionaryFile<? extends DictionaryItem> dictFile) {
try {
return ComponentUtil.getCurlHelper().get("/_configsync/file").param("path", dictFile.getPath()).execute().getContentAsStream();
} catch (final IOException e) {
throw new DictionaryException("Failed to access " + dictFile.getPath(), e);
}
public CurlResponse getContentResponse(final DictionaryFile<? extends DictionaryItem> dictFile) {
return ComponentUtil.getCurlHelper().get("/_configsync/file").param("path", dictFile.getPath()).execute();
}
public void addCreator(final DictionaryCreator creator) {

View file

@ -15,7 +15,6 @@
*/
package org.codelibs.fess.dict.kuromoji;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
@ -33,6 +32,7 @@ import java.util.List;
import org.codelibs.core.io.CloseableUtil;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.curl.CurlResponse;
import org.codelibs.fess.Constants;
import org.codelibs.fess.dict.DictionaryException;
import org.codelibs.fess.dict.DictionaryFile;
@ -61,7 +61,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
@Override
public synchronized OptionalEntity<KuromojiItem> get(final long id) {
if (kuromojiItemList == null) {
reload(null, null);
reload(null);
}
for (final KuromojiItem kuromojiItem : kuromojiItemList) {
@ -75,7 +75,7 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
@Override
public synchronized PagingList<KuromojiItem> selectList(final int offset, final int size) {
if (kuromojiItemList == null) {
reload(null, null);
reload(null);
}
if (offset >= kuromojiItemList.size() || offset < 0) {
@ -93,14 +93,14 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
@Override
public synchronized void insert(final KuromojiItem item) {
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@Override
public synchronized void update(final KuromojiItem item) {
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@ -109,14 +109,21 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
final KuromojiItem kuromojiItem = item;
kuromojiItem.setNewToken(StringUtil.EMPTY);
try (KuromojiUpdater updater = new KuromojiUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
protected void reload(final KuromojiUpdater updater) {
try (CurlResponse curlResponse = dictionaryManager.getContentResponse(this)) {
reload(updater, curlResponse.getContentAsStream());
} catch (final IOException e) {
throw new DictionaryException("Failed to parse " + path, e);
}
}
protected void reload(final KuromojiUpdater updater, final InputStream in) {
final List<KuromojiItem> itemList = new ArrayList<>();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(in != null ? in : dictionaryManager.getContentInputStream(this), Constants.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, Constants.UTF_8))) {
long id = 0;
String line = null;
while ((line = reader.readLine()) != null) {
@ -178,10 +185,6 @@ public class KuromojiFile extends DictionaryFile<KuromojiItem> {
return new File(path).getName();
}
public InputStream getInputStream() throws IOException {
return new BufferedInputStream(dictionaryManager.getContentInputStream(this));
}
public synchronized void update(final InputStream in) throws IOException {
try (KuromojiUpdater updater = new KuromojiUpdater(null)) {
reload(updater, in);

View file

@ -15,7 +15,6 @@
*/
package org.codelibs.fess.dict.mapping;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
@ -35,6 +34,7 @@ import java.util.regex.Pattern;
import org.codelibs.core.io.CloseableUtil;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.curl.CurlResponse;
import org.codelibs.fess.Constants;
import org.codelibs.fess.dict.DictionaryException;
import org.codelibs.fess.dict.DictionaryFile;
@ -66,7 +66,7 @@ public class CharMappingFile extends DictionaryFile<CharMappingItem> {
@Override
public OptionalEntity<CharMappingItem> get(final long id) {
if (mappingItemList == null) {
reload(null, null);
reload(null);
}
for (final CharMappingItem mappingItem : mappingItemList) {
@ -80,7 +80,7 @@ public class CharMappingFile extends DictionaryFile<CharMappingItem> {
@Override
public synchronized PagingList<CharMappingItem> selectList(final int offset, final int size) {
if (mappingItemList == null) {
reload(null, null);
reload(null);
}
if (offset >= mappingItemList.size() || offset < 0) {
@ -98,14 +98,14 @@ public class CharMappingFile extends DictionaryFile<CharMappingItem> {
@Override
public synchronized void insert(final CharMappingItem item) {
try (MappingUpdater updater = new MappingUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@Override
public synchronized void update(final CharMappingItem item) {
try (MappingUpdater updater = new MappingUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@ -115,15 +115,22 @@ public class CharMappingFile extends DictionaryFile<CharMappingItem> {
mappingItem.setNewInputs(StringUtil.EMPTY_STRINGS);
mappingItem.setNewOutput(StringUtil.EMPTY);
try (MappingUpdater updater = new MappingUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
protected void reload(final MappingUpdater updater) {
try (CurlResponse curlResponse = dictionaryManager.getContentResponse(this)) {
reload(updater, curlResponse.getContentAsStream());
} catch (final IOException e) {
throw new DictionaryException("Failed to parse " + path, e);
}
}
protected void reload(final MappingUpdater updater, final InputStream in) {
final Pattern parsePattern = Pattern.compile("(.*)\\s*=>\\s*(.*)\\s*$");
final List<CharMappingItem> itemList = new ArrayList<>();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(in != null ? in : dictionaryManager.getContentInputStream(this), Constants.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, Constants.UTF_8))) {
long id = 0;
String line = null;
while ((line = reader.readLine()) != null) {
@ -192,10 +199,6 @@ public class CharMappingFile extends DictionaryFile<CharMappingItem> {
return new File(path).getName();
}
public InputStream getInputStream() throws IOException {
return new BufferedInputStream(dictionaryManager.getContentInputStream(this));
}
public synchronized void update(final InputStream in) throws IOException {
try (MappingUpdater updater = new MappingUpdater(null)) {
reload(updater, in);

View file

@ -15,7 +15,6 @@
*/
package org.codelibs.fess.dict.protwords;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
@ -33,6 +32,7 @@ import java.util.List;
import org.codelibs.core.io.CloseableUtil;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.curl.CurlResponse;
import org.codelibs.fess.Constants;
import org.codelibs.fess.dict.DictionaryException;
import org.codelibs.fess.dict.DictionaryFile;
@ -60,7 +60,7 @@ public class ProtwordsFile extends DictionaryFile<ProtwordsItem> {
@Override
public synchronized OptionalEntity<ProtwordsItem> get(final long id) {
if (protwordsItemList == null) {
reload(null, null);
reload(null);
}
for (final ProtwordsItem ProtwordsItem : protwordsItemList) {
@ -74,7 +74,7 @@ public class ProtwordsFile extends DictionaryFile<ProtwordsItem> {
@Override
public synchronized PagingList<ProtwordsItem> selectList(final int offset, final int size) {
if (protwordsItemList == null) {
reload(null, null);
reload(null);
}
if (offset >= protwordsItemList.size() || offset < 0) {
@ -92,14 +92,14 @@ public class ProtwordsFile extends DictionaryFile<ProtwordsItem> {
@Override
public synchronized void insert(final ProtwordsItem item) {
try (ProtwordsUpdater updater = new ProtwordsUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@Override
public synchronized void update(final ProtwordsItem item) {
try (ProtwordsUpdater updater = new ProtwordsUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@ -108,14 +108,21 @@ public class ProtwordsFile extends DictionaryFile<ProtwordsItem> {
final ProtwordsItem ProtwordsItem = item;
ProtwordsItem.setNewInput(StringUtil.EMPTY);
try (ProtwordsUpdater updater = new ProtwordsUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
protected void reload(final ProtwordsUpdater updater) {
try (CurlResponse curlResponse = dictionaryManager.getContentResponse(this)) {
reload(updater, curlResponse.getContentAsStream());
} catch (final IOException e) {
throw new DictionaryException("Failed to parse " + path, e);
}
}
protected void reload(final ProtwordsUpdater updater, final InputStream in) {
final List<ProtwordsItem> itemList = new ArrayList<>();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(in != null ? in : dictionaryManager.getContentInputStream(this), Constants.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, Constants.UTF_8))) {
long id = 0;
String line = null;
while ((line = reader.readLine()) != null) {
@ -179,10 +186,6 @@ public class ProtwordsFile extends DictionaryFile<ProtwordsItem> {
return new File(path).getName();
}
public InputStream getInputStream() throws IOException {
return new BufferedInputStream(dictionaryManager.getContentInputStream(this));
}
public synchronized void update(final InputStream in) throws IOException {
try (ProtwordsUpdater updater = new ProtwordsUpdater(null)) {
reload(updater, in);

View file

@ -15,7 +15,6 @@
*/
package org.codelibs.fess.dict.stemmeroverride;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
@ -35,6 +34,7 @@ import java.util.regex.Pattern;
import org.codelibs.core.io.CloseableUtil;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.curl.CurlResponse;
import org.codelibs.fess.Constants;
import org.codelibs.fess.dict.DictionaryException;
import org.codelibs.fess.dict.DictionaryFile;
@ -66,7 +66,7 @@ public class StemmerOverrideFile extends DictionaryFile<StemmerOverrideItem> {
@Override
public synchronized OptionalEntity<StemmerOverrideItem> get(final long id) {
if (stemmerOverrideItemList == null) {
reload(null, null);
reload(null);
}
for (final StemmerOverrideItem stemmerOverrideItem : stemmerOverrideItemList) {
@ -80,7 +80,7 @@ public class StemmerOverrideFile extends DictionaryFile<StemmerOverrideItem> {
@Override
public synchronized PagingList<StemmerOverrideItem> selectList(final int offset, final int size) {
if (stemmerOverrideItemList == null) {
reload(null, null);
reload(null);
}
if (offset >= stemmerOverrideItemList.size() || offset < 0) {
@ -98,14 +98,14 @@ public class StemmerOverrideFile extends DictionaryFile<StemmerOverrideItem> {
@Override
public synchronized void insert(final StemmerOverrideItem item) {
try (StemmerOverrideUpdater updater = new StemmerOverrideUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@Override
public synchronized void update(final StemmerOverrideItem item) {
try (StemmerOverrideUpdater updater = new StemmerOverrideUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@ -115,15 +115,22 @@ public class StemmerOverrideFile extends DictionaryFile<StemmerOverrideItem> {
stemmerOverrideItem.setNewInput(StringUtil.EMPTY);
stemmerOverrideItem.setNewOutput(StringUtil.EMPTY);
try (StemmerOverrideUpdater updater = new StemmerOverrideUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
protected void reload(final StemmerOverrideUpdater updater) {
try (CurlResponse curlResponse = dictionaryManager.getContentResponse(this)) {
reload(updater, curlResponse.getContentAsStream());
} catch (final IOException e) {
throw new DictionaryException("Failed to parse " + path, e);
}
}
protected void reload(final StemmerOverrideUpdater updater, final InputStream in) {
final Pattern parsePattern = Pattern.compile("(.*)\\s*=>\\s*(.*)\\s*$");
final List<StemmerOverrideItem> itemList = new ArrayList<>();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(in != null ? in : dictionaryManager.getContentInputStream(this), Constants.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, Constants.UTF_8))) {
long id = 0;
String line = null;
while ((line = reader.readLine()) != null) {
@ -189,10 +196,6 @@ public class StemmerOverrideFile extends DictionaryFile<StemmerOverrideItem> {
return new File(path).getName();
}
public InputStream getInputStream() throws IOException {
return new BufferedInputStream(dictionaryManager.getContentInputStream(this));
}
public synchronized void update(final InputStream in) throws IOException {
try (StemmerOverrideUpdater updater = new StemmerOverrideUpdater(null)) {
reload(updater, in);

View file

@ -15,7 +15,6 @@
*/
package org.codelibs.fess.dict.stopwords;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
@ -33,6 +32,7 @@ import java.util.List;
import org.codelibs.core.io.CloseableUtil;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.curl.CurlResponse;
import org.codelibs.fess.Constants;
import org.codelibs.fess.dict.DictionaryException;
import org.codelibs.fess.dict.DictionaryFile;
@ -60,7 +60,7 @@ public class StopwordsFile extends DictionaryFile<StopwordsItem> {
@Override
public synchronized OptionalEntity<StopwordsItem> get(final long id) {
if (stopwordsItemList == null) {
reload(null, null);
reload(null);
}
for (final StopwordsItem StopwordsItem : stopwordsItemList) {
@ -74,7 +74,7 @@ public class StopwordsFile extends DictionaryFile<StopwordsItem> {
@Override
public synchronized PagingList<StopwordsItem> selectList(final int offset, final int size) {
if (stopwordsItemList == null) {
reload(null, null);
reload(null);
}
if (offset >= stopwordsItemList.size() || offset < 0) {
@ -92,14 +92,14 @@ public class StopwordsFile extends DictionaryFile<StopwordsItem> {
@Override
public synchronized void insert(final StopwordsItem item) {
try (SynonymUpdater updater = new SynonymUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@Override
public synchronized void update(final StopwordsItem item) {
try (SynonymUpdater updater = new SynonymUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@ -108,14 +108,21 @@ public class StopwordsFile extends DictionaryFile<StopwordsItem> {
final StopwordsItem StopwordsItem = item;
StopwordsItem.setNewInput(StringUtil.EMPTY);
try (SynonymUpdater updater = new SynonymUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
protected void reload(final SynonymUpdater updater) {
try (CurlResponse curlResponse = dictionaryManager.getContentResponse(this)) {
reload(updater, curlResponse.getContentAsStream());
} catch (final IOException e) {
throw new DictionaryException("Failed to parse " + path, e);
}
}
protected void reload(final SynonymUpdater updater, final InputStream in) {
final List<StopwordsItem> itemList = new ArrayList<>();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(in != null ? in : dictionaryManager.getContentInputStream(this), Constants.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, Constants.UTF_8))) {
long id = 0;
String line = null;
while ((line = reader.readLine()) != null) {
@ -179,10 +186,6 @@ public class StopwordsFile extends DictionaryFile<StopwordsItem> {
return new File(path).getName();
}
public InputStream getInputStream() throws IOException {
return new BufferedInputStream(dictionaryManager.getContentInputStream(this));
}
public synchronized void update(final InputStream in) throws IOException {
try (SynonymUpdater updater = new SynonymUpdater(null)) {
reload(updater, in);

View file

@ -15,7 +15,6 @@
*/
package org.codelibs.fess.dict.synonym;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
@ -33,6 +32,7 @@ import java.util.List;
import org.codelibs.core.io.CloseableUtil;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.curl.CurlResponse;
import org.codelibs.fess.Constants;
import org.codelibs.fess.dict.DictionaryException;
import org.codelibs.fess.dict.DictionaryFile;
@ -60,7 +60,7 @@ public class SynonymFile extends DictionaryFile<SynonymItem> {
@Override
public synchronized OptionalEntity<SynonymItem> get(final long id) {
if (synonymItemList == null) {
reload(null, null);
reload(null);
}
for (final SynonymItem synonymItem : synonymItemList) {
@ -74,7 +74,7 @@ public class SynonymFile extends DictionaryFile<SynonymItem> {
@Override
public synchronized PagingList<SynonymItem> selectList(final int offset, final int size) {
if (synonymItemList == null) {
reload(null, null);
reload(null);
}
if (offset >= synonymItemList.size() || offset < 0) {
@ -92,14 +92,14 @@ public class SynonymFile extends DictionaryFile<SynonymItem> {
@Override
public synchronized void insert(final SynonymItem item) {
try (SynonymUpdater updater = new SynonymUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@Override
public synchronized void update(final SynonymItem item) {
try (SynonymUpdater updater = new SynonymUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
@ -109,14 +109,21 @@ public class SynonymFile extends DictionaryFile<SynonymItem> {
synonymItem.setNewInputs(StringUtil.EMPTY_STRINGS);
synonymItem.setNewOutputs(StringUtil.EMPTY_STRINGS);
try (SynonymUpdater updater = new SynonymUpdater(item)) {
reload(updater, null);
reload(updater);
}
}
protected void reload(final SynonymUpdater updater) {
try (CurlResponse curlResponse = dictionaryManager.getContentResponse(this)) {
reload(updater, curlResponse.getContentAsStream());
} catch (final IOException e) {
throw new DictionaryException("Failed to parse " + path, e);
}
}
protected void reload(final SynonymUpdater updater, final InputStream in) {
final List<SynonymItem> itemList = new ArrayList<>();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(in != null ? in : dictionaryManager.getContentInputStream(this), Constants.UTF_8))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, Constants.UTF_8))) {
long id = 0;
String line = null;
while ((line = reader.readLine()) != null) {
@ -250,10 +257,6 @@ public class SynonymFile extends DictionaryFile<SynonymItem> {
return new File(path).getName();
}
public InputStream getInputStream() throws IOException {
return new BufferedInputStream(dictionaryManager.getContentInputStream(this));
}
public synchronized void update(final InputStream in) throws IOException {
try (SynonymUpdater updater = new SynonymUpdater(null)) {
reload(updater, in);