Skip to content

Commit 639afc7

Browse files
committed
removed code smells
1 parent 0cc5a19 commit 639afc7

File tree

133 files changed

+1187
-357
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+1187
-357
lines changed

logicaldoc-cmis/src/test/resources/contexttest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434

3535
<bean id="ContextProperties" class="com.logicaldoc.util.config.ContextProperties" />
3636

37-
<bean id="Context" class="com.logicaldoc.util.Context" abstract="false"
38-
lazy-init="default" autowire="default" />
37+
<bean id="Context" class="com.logicaldoc.util.Context" factory-method="get" lazy-init="false" />
3938

4039
<!-- DataSource -->
4140
<bean id="HikariConfig" class="com.zaxxer.hikari.HikariConfig">

logicaldoc-core/src/main/java/com/logicaldoc/core/NamedParameterStatement.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,20 @@ private static final String parse(String query, Map<String, List<Integer>> param
109109
boolean inDoubleQuote = false;
110110
int index = 1;
111111

112-
for (int i = 0; i < length; i++) {
112+
int i = 0;
113+
while (i < length) {
113114
char c = query.charAt(i);
114-
if (inSingleQuote) {
115-
if (c == '\'') {
116-
inSingleQuote = false;
117-
}
118-
} else if (inDoubleQuote) {
119-
if (c == '"') {
120-
inDoubleQuote = false;
121-
}
115+
if (inSingleQuote && c == '\'') {
116+
inSingleQuote = false;
117+
} else if (inDoubleQuote && c == '"') {
118+
inDoubleQuote = false;
122119
} else {
123120
if (c == '\'') {
124121
inSingleQuote = true;
125122
} else if (c == '"') {
126123
inDoubleQuote = true;
127124
} else if (c == ':' && i + 1 < length && Character.isJavaIdentifierStart(query.charAt(i + 1))) {
128-
int j = i + 2;
129-
while (j < length && Character.isJavaIdentifierPart(query.charAt(j)))
130-
j++;
131-
String name = query.substring(i + 1, j);
125+
String name = getParameterName(query, i);
132126
c = '?'; // replace the parameter with a question mark
133127
i += name.length(); // skip past the end if the parameter
134128

@@ -137,12 +131,21 @@ private static final String parse(String query, Map<String, List<Integer>> param
137131
}
138132
}
139133
parsedQuery.append(c);
134+
i++;
140135
}
141136

142137
return parsedQuery.toString();
143138

144139
}
145140

141+
private static String getParameterName(String query, int currentPosition) {
142+
int j = currentPosition + 2;
143+
while (j < query.length() && Character.isJavaIdentifierPart(query.charAt(j)))
144+
j++;
145+
String name = query.substring(currentPosition + 1, j);
146+
return name;
147+
}
148+
146149
/**
147150
* Returns the indexes for a parameter.
148151
*

logicaldoc-core/src/main/java/com/logicaldoc/core/document/AbstractDocument.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public abstract class AbstractDocument extends SecurableExtensibleObject impleme
112112
public static final int NATURE_DOC = 0;
113113

114114
private String comment;
115-
115+
116116
/**
117117
* The text of the last note put on the document
118118
*/
@@ -929,40 +929,12 @@ public boolean isGranted(String myPassword) {
929929

930930
try {
931931
String test = CryptUtil.encryptSHA256(myPassword);
932-
if (test.equals(getPassword()))
933-
return true;
934-
935-
// The test with current algorithm failed so we try with the legacy
936-
// one
937-
String testLegacy = CryptUtil.encryptSHA(myPassword);
938-
if (testLegacy.equals(getPassword())) {
939-
// There is match with the old scheme, so update the database
940-
// with the new one
941-
setPassword(test);
942-
fixEncryptedPassword(test);
943-
return true;
944-
}
945-
946-
return false;
932+
return test.equals(getPassword());
947933
} catch (Exception t) {
948934
return false;
949935
}
950936
}
951937

952-
/**
953-
* Saves the password encrypted with current algorithm into the database
954-
*
955-
* @param encryptedPassword The encrypted password to write
956-
*/
957-
private void fixEncryptedPassword(String encryptedPassword) {
958-
try {
959-
DocumentDAO docDao = Context.get(DocumentDAO.class);
960-
docDao.jdbcUpdate("update ld_document set ld_password='" + encryptedPassword + "' where ld_id=" + getId());
961-
} catch (PersistenceException e) {
962-
log.warn(e.getMessage());
963-
}
964-
}
965-
966938
public boolean isPasswordProtected() {
967939
return StringUtils.isNotEmpty(getPassword());
968940
}

logicaldoc-core/src/main/java/com/logicaldoc/core/document/HibernateDocumentNoteDAO.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public void store(DocumentNote note) throws PersistenceException {
5050
}
5151

5252
private void updateLastNote(Document doccument, DocumentNote note) {
53-
// In case of note on the whole document, update the document's lastNote field
53+
// In case of note on the whole document, update the document's lastNote
54+
// field
5455
if (note.getPage() == 0)
5556
ThreadPools.get().execute(() -> {
5657
try {
@@ -115,20 +116,24 @@ public List<DocumentNote> findByDocIdAndTypes(long docId, String fileVersion, Co
115116
params.put(DOC_ID, docId);
116117
params.put("types", types);
117118

118-
return findByWhere(ENTITY + DOC_ID_DOC_ID_AND + ENTITY + ".type in (:types)", params, null, null);
119+
return findByWhere(
120+
ENTITY + DOC_ID_DOC_ID_AND + ENTITY + ".type in (:types) and " + ENTITY + ".deleted=0", params,
121+
null, null);
119122
}
120123
} else if (types == null || types.isEmpty()) {
121124
Map<String, Object> params = new HashMap<>();
122125
params.put(DOC_ID, docId);
123126
params.put("fileVersion", fileVersion);
124-
return findByWhere(ENTITY + DOC_ID_DOC_ID_AND + ENTITY + ".fileVersion = :fileVersion", params, null, null);
127+
return findByWhere(
128+
ENTITY + DOC_ID_DOC_ID_AND + ENTITY + ".fileVersion = :fileVersion and " + ENTITY + ".deleted=0",
129+
params, null, null);
125130
} else {
126131
Map<String, Object> params = new HashMap<>();
127132
params.put(DOC_ID, docId);
128133
params.put("fileVersion", fileVersion);
129134
params.put("types", types);
130135
return findByWhere(ENTITY + DOC_ID_DOC_ID_AND + ENTITY + ".fileVersion = :fileVersion and " + ENTITY
131-
+ ".type in (:types)", params, null, null);
136+
+ ".type in (:types) and " + ENTITY + ".deleted=0", params, null, null);
132137
}
133138
}
134139

@@ -137,7 +142,6 @@ public List<DocumentNote> findByUserId(long userId) throws PersistenceException
137142
return findByWhere(ENTITY + ".userId =" + userId, "order by " + ENTITY + ".date desc", null);
138143
}
139144

140-
141145
@Override
142146
public void delete(long id, int code) throws PersistenceException {
143147
DocumentNote note = findById(id);

logicaldoc-core/src/main/java/com/logicaldoc/core/security/authentication/ApiKeyAuthenticator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
import java.util.Date;
55
import java.util.Map;
66

7-
import javax.annotation.Resource;
8-
97
import org.apache.commons.lang3.StringUtils;
108
import org.slf4j.Logger;
119
import org.slf4j.LoggerFactory;
10+
import org.springframework.beans.factory.annotation.Autowired;
1211
import org.springframework.stereotype.Component;
1312

1413
import com.logicaldoc.core.PersistenceException;
1514
import com.logicaldoc.core.security.Client;
1615
import com.logicaldoc.core.security.apikey.ApiKey;
1716
import com.logicaldoc.core.security.apikey.ApiKeyDAO;
1817
import com.logicaldoc.core.security.user.User;
18+
import com.logicaldoc.core.security.user.UserDAO;
1919

2020
/**
2121
* This authenticator uses the API Key
@@ -28,10 +28,11 @@ public class ApiKeyAuthenticator extends DefaultAuthenticator {
2828

2929
protected static Logger log = LoggerFactory.getLogger(ApiKeyAuthenticator.class);
3030

31-
@Resource(name = "ApiKeyDAO")
3231
protected ApiKeyDAO apiKeyDAO;
3332

34-
public void setApiKeyDAO(ApiKeyDAO apiKeyDAO) {
33+
@Autowired
34+
public ApiKeyAuthenticator(UserDAO userDAO, ApiKeyDAO apiKeyDAO) {
35+
super(userDAO);
3536
this.apiKeyDAO = apiKeyDAO;
3637
}
3738

logicaldoc-core/src/main/java/com/logicaldoc/core/security/authentication/DefaultAuthenticator.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import java.security.NoSuchAlgorithmException;
44

5-
import javax.annotation.Resource;
6-
7-
import org.apache.commons.lang3.StringUtils;
85
import org.slf4j.Logger;
96
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
108
import org.springframework.stereotype.Component;
119

1210
import com.logicaldoc.core.PersistenceException;
@@ -27,10 +25,11 @@ public class DefaultAuthenticator extends AbstractAuthenticator {
2725

2826
protected static Logger log = LoggerFactory.getLogger(DefaultAuthenticator.class);
2927

30-
@Resource(name = "UserDAO")
3128
protected UserDAO userDAO;
3229

33-
public void setUserDAO(UserDAO userDAO) {
30+
@Autowired
31+
public DefaultAuthenticator(UserDAO userDAO) {
32+
super();
3433
this.userDAO = userDAO;
3534
}
3635

@@ -53,25 +52,15 @@ public User authenticate(String username, String password, String key, Client cl
5352

5453
// Check the password match with one of the current or legacy algorithm
5554
String test = null;
56-
String testLegacy = null;
5755
try {
5856
test = CryptUtil.encryptSHA256(password);
59-
testLegacy = CryptUtil.encryptSHA(password);
6057
} catch (NoSuchAlgorithmException e) {
6158
log.error(e.getMessage(), e);
6259
}
6360

64-
if (user.getPassword() == null || (!user.getPassword().equals(test) && !user.getPassword().equals(testLegacy)))
61+
if (user.getPassword() == null || !user.getPassword().equals(test))
6562
throw new WrongPasswordException(this);
6663

67-
try {
68-
// Make sure the password in the DB follows the current scheme
69-
if (StringUtils.isNotEmpty(test) && user.getPassword().equals(testLegacy))
70-
userDAO.jdbcUpdate("update ld_user set ld_password='" + test + "' where ld_id = " + user.getId());
71-
} catch (PersistenceException e) {
72-
log.warn(e.getMessage());
73-
}
74-
7564
return user;
7665
}
7766

@@ -89,14 +78,13 @@ public User pickUser(String username) throws PersistenceException {
8978
* @throws AuthenticationException I something did not pass
9079
*/
9180
public void validateUser(User user) throws AuthenticationException {
92-
if (user == null) {
81+
if (user == null)
9382
throw new AccountNotFoundException(this);
94-
}
9583

9684
// Check the type
9785
if (user.getType() != User.TYPE_DEFAULT && user.getType() != User.TYPE_READONLY)
9886
throw new AccountTypeNotAllowedException();
99-
87+
10088
try {
10189
userDAO.initialize(user);
10290
} catch (PersistenceException e) {

logicaldoc-core/src/main/java/com/logicaldoc/core/security/spring/AdminAuthenticationProvider.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.logicaldoc.core.security.spring;
22

3-
import java.security.NoSuchAlgorithmException;
43
import java.util.ArrayList;
54
import java.util.Collection;
65

@@ -18,7 +17,6 @@
1817
import com.logicaldoc.core.security.user.UserDAO;
1918
import com.logicaldoc.util.Context;
2019
import com.logicaldoc.util.config.ContextProperties;
21-
import com.logicaldoc.util.crypt.CryptUtil;
2220

2321
/**
2422
* This Authentication provider extends the standard
@@ -46,7 +44,6 @@ public AdminAuthenticationProvider() {
4644
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
4745
UsernamePasswordAuthenticationToken auth = (UsernamePasswordAuthenticationToken) authentication;
4846
String username = String.valueOf(auth.getPrincipal());
49-
String password = String.valueOf(auth.getCredentials());
5047

5148
if (!ADMIN.equals(username))
5249
throw new BadCredentialsException(BADCREDENTIALS);
@@ -92,16 +89,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
9289
if (adminPasswd == null || adminPasswd.isEmpty())
9390
throw new BadCredentialsException(BADCREDENTIALS);
9491

95-
// Check the password match with one of the current or legacy algorithm
96-
String testLegacy = "";
97-
try {
98-
testLegacy = CryptUtil.encryptSHA(password);
99-
user.setDecodedPassword(password);
100-
} catch (NoSuchAlgorithmException e) {
101-
log.error("Cannot cript the password", e);
102-
}
103-
104-
if (!user.getPassword().equals(adminPasswd) && !testLegacy.equals(adminPasswd))
92+
if (!user.getPassword().equals(adminPasswd))
10593
throw new BadCredentialsException(BADCREDENTIALS);
10694

10795
Collection<GrantedAuthority> authorities = new ArrayList<>();

logicaldoc-core/src/test/java/com/logicaldoc/core/dashlet/DashletContentTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public void testService() throws IOException {
9696
response = new MockServletResponse(responseFile);
9797
testSubject.service(mockRequest, response);
9898
response.flushBuffer();
99-
System.out.println(FileUtil.readFile(responseFile));
10099
assertEquals("", FileUtil.readFile(responseFile));
101100

102101
}

logicaldoc-core/src/test/resources/context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
<bean id="ContextProperties" class="com.logicaldoc.util.config.ContextProperties" />
3636

37-
<bean id="Context" class="com.logicaldoc.util.Context" />
37+
<bean id="Context" class="com.logicaldoc.util.Context" factory-method="get" lazy-init="false" />
3838

3939
<!-- DataSource -->
4040
<bean id="HikariConfig" class="com.zaxxer.hikari.HikariConfig">

logicaldoc-dropbox/src/test/resources/context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
class="com.logicaldoc.util.config.ContextProperties">
3636
</bean>
3737

38-
<bean id="Context" class="com.logicaldoc.util.Context"
38+
<bean id="Context" class="com.logicaldoc.util.Context" factory-method="get" lazy-init="false"
3939
abstract="false" lazy-init="default" autowire="default"
4040
/>
4141

0 commit comments

Comments
 (0)